(String). Word Pattern
2016-04-12 15:26
302 查看
Given a
Here follow means a full match, such that there is a bijection between a letter in
Examples:
pattern =
pattern =
pattern =
pattern =
patternand a string
str, find if
strfollows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in
patternand a non-empty word in
str.
Examples:
pattern =
"abba", str =
"dog cat cat dog"should return true.
pattern =
"abba", str =
"dog cat cat fish"should return false.
pattern =
"aaaa", str =
"dog cat cat dog"should return false.
pattern =
"abba", str =
"dog dog dog dog"should return false
public class Solution { //if不用hashmap,更好的方法是设置头尾“指针”,保证一个指向当前的值 public boolean wordPattern(String pattern, String str) { String[] strs = str.split(" "); if (pattern.length() != strs.length) return false; Map<Character, String> map = new HashMap<Character, String>(); for (int i = 0; i < pattern.length(); i++) { if (map.containsKey(pattern.charAt(i)) && !(map.get(pattern.charAt(i))).equals(strs[i])) return false; if (!map.containsKey(pattern.charAt(i)) && map.containsValue(strs[i])) return false; if (!map.containsKey(pattern.charAt(i)) && !map.containsValue(strs[i])) map.put(pattern.charAt(i), strs[i]); } return true; } }
相关文章推荐
- PHP函数的实现原理及性能分析[2010年前文章]
- SDUT OJ 3328 JAVA判断合法标识符
- Android在线支付
- 结对编程项目进展——第四周
- 修改后的取得汉字首字母的lazarus函数,可以自己增加疑难汉字,这个应该比较理想了
- Activity生命周期
- DFS
- 链式队列
- thinkphp输出表格
- MySQL查询指定时间的数据
- zookeeper应用场景-java
- 文件知识
- mfcs100d.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in MSVCRTD.lib(dllmain.obj)
- 作为一个菜鸟女大学生
- Asp.net mvc 控制器
- 剑指Offer--015-链表中倒数第k个结点
- Android TypedArray详情
- MyEclipse中properties文件乱码的解决办法之安装properties插件
- 7 libjpeg使用
- RxAndroid的初体验