[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:
pattern
contains only lowercase alphabetical letters, andstr
contains words separated by a single space. Each word instr
contains only lowercase alphabetical letters.- Both
pattern
andstr
do not have leading or trailing spaces. - Each letter in
pattern
must 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 ...
随机推荐
- BZOJ 4423: [AMPPZ2013]Bytehattan
Sol 对偶图+并查集. 思路非常好,将网格图转化成对偶图,在原图中删掉一条边,相当于在对偶图中连上一条边(其实就是网格的格点相互连边),每次加边用并查集维护就可以了. 哦对,还要注意边界就是网格外面 ...
- Linux下多网卡同网段多IP网络分流设定方法
Linux下多网卡同网段多IP网络分流设定方法 -- :: 标签:Linux下多网卡同网段多IP网络分流设定方法 当服务器需要较高的网络流量时,在其它资源不造成瓶颈的情况下无疑会用到多网卡. 第1选项 ...
- 腾讯云环境配置之PHP5.6.3 + redis扩展 稳定版
腾讯云环境配置之PHP5.6.3 + redis扩展 稳定版 时间:2015-01-18 01:41来源:linux.it.net.cn 作者:IT #由于上文装过yum groupinstall ...
- 百度云+ KeePass 网络同步你的密码
百度云+ KeePass 网络同步你的密码 百度云一个目前不限流量不限格式能直链的网盘,速度在我这里很快,难得了!KeePass(小众介绍过 KeePass.) 是一个免费开源的密码管理类软件, ...
- IntelliJ IDEA 常用快捷键列表及技巧大全
IntelliJ Idea 常用快捷键列表 Alt+回车 导入包,自动修正Ctrl+N 查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L 格式化代码Ctrl+Alt+O 优化导入的类和 ...
- C# 毕业证书打印《三》
打印很关键的方法,打印方法DataPrint(),将你要打印的数据信息发送到打印机就可以了,打印机将自动处理. public void DataPrint() { try { PrintDocumen ...
- 常见计算机基础笔试题总结quickstart
[本文链接] 1. 以下是一颗平衡二叉树,请画出插入键值3以后的这颗平衡二叉树. 分析:考察平衡二叉树的基本操作,插入3变成不平衡,需要节点5右旋一次,节点2左旋一次.. 2. 表达式X=A+(B*( ...
- 在VS中MFC、ATL与WIN32有什么联系或区别?
有时候遇到一些初学者问我这个问题:在VS中使用MFC和ATL与使用WIN32有什么联系或区别?通俗来说,win32是通过调用windows api去实现需要的功能.而MFC和ATL是封装好的类库,包含 ...
- javascript 事件委托,jq,js模拟事件
<!DOCTYPE> <html> <head> <title></title> <script src="Scripts/ ...
- Selenium webdriver 操作日历控件
一般的日期控件都是input标签下弹出来的,如果使用webdriver 去设置日期, 1. 定位到该input 2. 使用sendKeys 方法 比如: 但是,有的日期控件是readonly的 比如1 ...