(String). 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.
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 falsepublic 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;
}
}
(String). 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 ...
- 290. Word Pattern
题目: 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 ...
随机推荐
- Myelipcse导入Maven项目: version of spring facet could not be detected
在导入已存在的maven web项目的时候,总是出现Versions of Spring facet could not be detected的问题. 查资料知道有两种解决方法:一个是什么在项目根目 ...
- 简单的射击游戏HTML+JS实现
一直想自己写一个游戏玩,时间和精力都不太允许,最近几天刚好有空闲时间,就琢磨了这个小游戏. 刚开始想着计算图片重叠事件,然后让炮弹和飞机消失,傻乎乎写了一天,越整越乱.今天一大早晕过来了,改用数组以后 ...
- php 获取静态方法调用的类名
方法一: class foo { static public function test() { var_dump(get_called_class()); } } class bar extends ...
- mysql数据库性能篇
慢查询:超过设定时间的SQL语句会被记录到指定文件内 1.观察mysql慢查询默认的时间(默认10秒) show variables like 'long%'; 2.修改慢查询设定时间 set lon ...
- Oracle字符集修改
1.使用管理员账号登录到oracle C:\Users\Administrator>sqlplus / as sysdba 2.查看字符集 SQL>select userenv('lang ...
- 求第K大数
1.排序找第K个数 2.快速排序分块 时间复杂度 2呢
- textarea光标处插入文字
(function($) { $.fn.extend({ //myField 对象元素 myValue 插入值 insertAtCursor: function(myField,myValue) { ...
- Web应用功能测试测试点
做了几年的功能测试,也经手了好几个Web应用的项目,在做项目当中积累了一些经验.在这里我对通用一些功能模块的测试点做个记录,一来梳理一下测试用例设计的思路,以便加快相似项目的测试用例的设计,二来有利于 ...
- EXT.net DateField format设置
DateField df = new DateField(); df.Format = "yyyy-MM-dd HH:mm:ss";格 ...
- Postgresql FATAL: could not create semaphores: No space left on device
昨天安装完成pg 9.5后,启动报错: FATAL: could not create semaphores: No space left on device DETAIL: Failed sys ...