Leetcode - Word Pattern
来源:互联网 发布:蒙特卡洛算法量化 编辑:程序博客网 时间:2024/06/09 19:25
Question
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.
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.
Notes
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
Java Code
//将pattern中的每个字符与str中对应的单词构成一个<key, value>键值对,依次存入map中,//如果遇到与map中已有的映射关系相矛盾的键值对,则判断为假public boolean wordPattern(String pattern, String str) { String[] value = str.split(" "); int len = pattern.length(); if(value.length != len) return false; HashMap<String, String> map = new HashMap<String, String>(); for(int i = 0; i < len; ++i) { String key = pattern.charAt(i) + ""; if(map.containsKey(key)) { if(!value[i].equals(map.get(key))) return false; }else { if(map.containsValue(value[i])) return false; map.put(key, value[i]); } } return true;}
0 0
- leetcode | Word Pattern
- 【leetcode】290. Word Pattern
- leetcode 290: Word Pattern
- [Leetcode]Word Pattern
- [leetcode 290]Word Pattern
- LeetCode 290 Word Pattern
- 【leetcode】290. Word Pattern
- [LeetCode 290] Word Pattern
- leetcode 290 Word Pattern
- LeetCode 2 Word Pattern
- leetcode word-pattern
- leetcode: Word Pattern
- leetcode:Word Pattern
- [Leetcode]Word Pattern
- LeetCode Word Pattern
- LeetCode-Word Pattern
- LeetCode:Word Pattern
- leetcode之Word Pattern
- mysql update between and
- 【Netty4 简单项目实践】六、断掉未鉴权的TCP长连接--ChannelHandelContext中的定时器用法
- jsp 文件上传下载
- jsp页显示时间标签JSTL标签 <fmt:formatDate/> 实例大全
- 科学家通过解码意念让四肢瘫痪患者弹起吉他
- Leetcode - Word Pattern
- 关于apache-commons-fileupload 的一个异常
- 安卓aidl的详细讲解(终极讲解)
- 指针和引用的区别
- 如何实现XA式、非XA式Spring分布式事务
- 【HUSTOJ】1051: 字符图形7-星号菱形
- web项目设置session监听器,统计同时在线人数
- IO通信模型和Netty 下篇
- 练习正则表达式与Date类的日期变字符串和字符串变日期