LeetCode_290. Word Pattern
290. Word Pattern
Given a pattern and a string str, find if str follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
Example 1:
Input: pattern ="abba", str ="dog cat cat dog"
Output: true
Example 2:
Input:pattern ="abba", str ="dog cat cat fish"
Output: false
Example 3:
Input: pattern ="aaaa", str ="dog cat cat dog"
Output: false
Example 4:
Input: pattern ="abba", str ="dog dog dog dog"
Output: false
Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters that may be separated by a single space.
package leetcode.easy;
public class WordPattern {
@org.junit.Test
public void test() {
String pattern1 = "abba";
String str1 = "dog cat cat dog";
String pattern2 = "abba";
String str2 = "dog cat cat fish";
String pattern3 = "aaaa";
String str3 = "dog cat cat dog";
String pattern4 = "abba";
String str4 = "dog dog dog dog";
System.out.println(wordPattern(pattern1, str1));
System.out.println(wordPattern(pattern2, str2));
System.out.println(wordPattern(pattern3, str3));
System.out.println(wordPattern(pattern4, str4));
}
public boolean wordPattern(String pattern, String str) {
String[] arrays = str.split(" ");
if (arrays.length != pattern.length()) {
return false;
}
java.util.Map<String, Character> map = new java.util.HashMap<>();
for (int i = 0; i < pattern.length(); i++) {
if (map.containsKey(arrays[i]) && map.get(arrays[i]) != pattern.charAt(i)) {
return false;
} else if (!map.containsKey(arrays[i]) && map.containsValue(pattern.charAt(i))) {
return false;
} else {
map.put(arrays[i], pattern.charAt(i));
}
}
return true;
}
}
LeetCode_290. Word Pattern的更多相关文章
- [LeetCode] Word Pattern II 词语模式之二
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [LeetCode] Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...
- [LeetCode] Word Pattern
Word Pattern Total Accepted: 4627 Total Submissions: 17361 Difficulty: Easy Given a pattern and a st ...
- Word Pattern | & II
Word Pattern | Given a pattern and a string str, find if str follows the same pattern. Examples: pat ...
- 291. Word Pattern II
题目: Given a pattern and a string str, find if str follows the same pattern. Here follow means a full ...
- leetcode面试准备: Word Pattern
leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...
- Word Pattern
package cn.edu.xidian.sselab.hashtable; import java.util.HashMap;import java.util.Map; /** * * @au ...
- Word Pattern II 解答
Question Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
- 【leetcode】290. Word Pattern
problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...
随机推荐
- 移动端videojs视频插件使用直播流rtmp、hls、http-flv的注意事项
可以访问:https://videojs.com/ 下载对应的脚本包 特别注意的是 移动端videojs一般应用的直播流协议为HLS, RTMP协议一般是PC上使用,需要flash支持. HLS直播源 ...
- JVM对象创建
1.JVM对象创建:java程序运行过程中,无时无刻都有对象被创建出来.在语言层面上就是new关键字. 2.JVM对象创建过程: (1)JVM遇到一条new指令后,首先会去常量池中,检查这个指令的参数 ...
- 使用bufio包和函数式变成实现类似python生成器效果
package main import ( "bufio" "fmt" "io" "strings" ) type in ...
- jumpserver 安装
# CentOS 7 安装jumpserver $ setenforce 0 # 可以设置配置文件永久关闭$ systemctl stop iptables.service$ systemctl st ...
- dp--01背包,完全背包,多重背包
背包问题 以下代码 n是物品个数,m是背包容积 物品价值和重量int v[maxn],w[maxn]; 01背包 模板 for(int i = 0; i < n; i++) { for(int ...
- am335x system upgrade kernel can(八)
1 Scope of Document This document describes can bus hardware design and can bus driver developm ...
- noi.ac #37 dp计数
#include<algorithm> #include<cstring> #include<cstdio> #include<iostream> ty ...
- note_4.10
单位根反演 \[ \frac{1}{k}\sum_{i=0}^{k-1}\omega_k^{in}=[k|n] \] 所以 \[ \begin{equation} \begin{split} \sum ...
- docker中部署django项目~~Dockfile方式和compose方式
1. 背景: 本机win10上,后端django框架代码与前端vue框架代码联调通过. 2. 目的: 在centos7系统服务器上使用docker容器部署该项目. 3. 方案一:仅使用基 ...
- 44个Java性能优化
44个Java性能优化 首先,代码优化的目标是: 减小代码的体积 提高代码运行效率 代码优化细节 1 .尽量指定类.方法的final修饰符 带有final修饰符的类是不可派生的.在Java核心AP ...