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的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...
随机推荐
- strtol函数的用法——字符串转长整形
/* strtol example */ #include <stdio.h> /* printf */ #include <stdlib.h> /* strtol */ in ...
- PostgreSQL 输出 JSON 结果
PostGreSQL 从 9.2 开始增加对 JSON 的支持.9.5 已经支持多个 JSON 函数,见 http://www.postgres.cn/docs/9.5/functions-json. ...
- leetcode解题报告(25):Reverse Words in a String III
描述 Given a string, you need to reverse the order of characters in each word within a sentence while ...
- Luogu P3810 【模板】三维偏序(陌上花开) CDQ分治 树状数组
https://www.luogu.org/problemnew/show/P3810 复习板子,重要的题就真的要写三遍???之前写过一篇博客了,不过之前写的那个板子的sort用的规则真是沙雕 #in ...
- 关灯问题II 状压DP
关灯问题II 状压DP \(n\)个灯,\(m\)个按钮,每个按钮都会对每个灯有不同影响,问最少多少次使灯熄完. \(n\le 10,m\le 100\) 状压DP的好题,体现了状压的基本套路与二进制 ...
- 洛谷 P1257 平面上的最接近点对 题解
P1257 平面上的最接近点对 题目描述 给定平面上n个点,找出其中的一对点的距离,使得在这n个点的所有点对中,该距离为所有点对中最小的. 输入格式 第一行:n:2≤n≤10000 接下来n行:每行两 ...
- 汇编语言DOSBox软件使用方法
https://wenku.baidu.com/view/e63b8b46ccbff121dc368305.html
- Ceph osd故障恢复
1 调高osd的日志等级 加上红框那一行就可以了 osd的日志路径:/var/log/ceph/ceph-osd.3.log 注意:加上了这一行后日志会刷很多,所以要特别注意日志容量的变化,以防把v ...
- mysql设置主键自增长和自增长初始值
本文主要向大家介绍MySQL数据库之Mysql创建表实现主键自增并且初始值为200,希望对大家学习MySQL数据库有所帮助. 假设已经创建表test,并且主键为id.Mysql ...
- 模板 - 数学 - 同余 - 扩展Euclid算法
普通的扩展欧几里得算法,通过了洛谷的扩展欧几里得算法找乘法逆元.修复了容易溢出的bug,虽然新版本仍有可能会溢出longlong,假如参与运算的数字都是longlong,假如可以的话直接使用__int ...