[LeetCode] Word Pattern
Word Pattern
Given a pattern and a string str, find if str follows the same pattern.
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:
patterncontains only lowercase alphabetical letters, andstrcontains words separated by a single space. Each word instrcontains only lowercase alphabetical letters.- Both
patternandstrdo not have leading or trailing spaces. - Each letter in
patternmust map to a word with length that is at least 1.
Credits:
Special thanks to @minglotus6 for adding this problem and creating all test cases.
/**
* @param {string} pattern
* @param {string} str
* @return {boolean}
*/
var wordPattern = function(pattern, str) {
var s_to_p = {};
var p_to_s = {};
var pi = 0;
var si = 0;
if (!pattern || !str) return false;
while (pi < pattern.length) {
if (si >= str.length) return false;
var word = "";
var pt = pattern[pi];
while (si < str.length && str[si] != " ") {
word += str[si];
si++;
}
si++;
if (!s_to_p[word]) {
s_to_p[word] = pattern[pi];
}
else {
if (s_to_p[word] != pattern[pi]) return false;
} if (!p_to_s[pt]) {
p_to_s[pt] = word;
}
else {
if (p_to_s[pt] != word) return false;
}
pi++;
}
if (si < str.length) return false;
return true;
};
最简单的方法利用两个哈希表做双向的对应,主要容易错的是在各种edge cases,比如pattern 和 str 长度不对应,为空等。
[LeetCode] 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 II
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- LeetCode Word Pattern (模拟)
题意: 给出一个模式串pattern,再给出一个串str,问str的模板是否是pattern. 思路: 注意点:只要对于所有pattern[i]相同的i,str中对应的所有words[i]也必须相同, ...
- Leetcode solution 291: Word Pattern II
Problem Statement Given a pattern and a string str, find if str follows the same pattern. Here follo ...
- leetcode面试准备: Word Pattern
leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...
- LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)
翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...
- [LeetCode] 290. Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [LeetCode] 290. Word Pattern 单词模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
随机推荐
- 2016年10月10日--穷举、迭代、while循环
穷举 将所有可能性全部全部走一遍,使用IF筛选出满足的情况 练习: 1.单位给发了一张150元购物卡, 拿着到超市买三类洗化用品. 洗发水15元,香皂2元,牙刷5元. 求刚好花完150元,有多少种买法 ...
- Python自动化之sqlalchemy(修改和查询)
修改 my_user = Session.query(User).filter_by(name="alex").first() my_user.name = "Alex ...
- 2016-03-12 Leanning Plan
1,Python 2,Java 3,Html+Css 4,PHP 5,Crawl 6,WetChat Platform
- jquery验证手机号码和固定电话号码
<pre name="code" class="javascript"> //验证手机号码或者电话号码 function checkContactN ...
- [转载]能不能同时用static和const修饰类的成员函数?
题目(一):我们可以用static修饰一个类的成员函数,也可以用const修饰类的成员函数(写在函数的最后表示不能修改成员变量,不是指写在前面表示返回值为常量).请问:能不能同时用static和con ...
- C++构造函数、析构函数与抛出异常
[本文链接] http://www.cnblogs.com/hellogiser/p/constructor-destructor-exceptions.html [问题] 构造函数可以抛出异常么?析 ...
- redis 异常解决办法
redis 异常解决办法 26069:M 08 Aug 17:06:58.858 # WARNING: The TCP backlog setting of 511 cannot be enforce ...
- AC自动机题目汇总
POJ 4052 ZJU 3430 HDU 4117 HNU 10104 HDU 2457 HNU 11187 ZJU 3545 HDU 3341
- Unity3d《Shader篇》变胖
变胖前 变胖后 //Shader Shader "Custom/NormalExt" { Properties { _MainTex("Base (RGB)", ...
- iOS 在UITableViewCell中加入自定义view时view的frame设定注意
由于需要重用同一个布局,于是在cellForRowAtIndexPath中把自定义view加在了cell上,我是这样设定view的frame的 var screenFrame = UIScreen.m ...