290. Word Pattern
2015-11-30 15:51
176 查看
Given a
find if
Here follow means a full match, such that there is a bijection between a letter in
a non-empty word in
Examples:
pattern =
pattern =
pattern =
pattern =
Notes:
You may assume
lowercase letters separated by a single space.
Credits:
Special thanks to @minglotus6 for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
Hide Tags
Hash Table
Show Similar Problems
每个字母对应一个单词,且不同字母要对应不同的单词。
所以使用两个哈希表(一开始没注意,不同字母要对应不同的单词),一个用来记录字母和单词的对应,一个用来记录单词是否已经出现过。
与205. Isomorphic Strings几乎是一样的题。
另外,注意文中分词的方法,结合
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.
Notes:
You may assume
patterncontains only lowercase letters, and
strcontains
lowercase letters separated by a single space.
Credits:
Special thanks to @minglotus6 for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
Hide Tags
Hash Table
Show Similar Problems
每个字母对应一个单词,且不同字母要对应不同的单词。
所以使用两个哈希表(一开始没注意,不同字母要对应不同的单词),一个用来记录字母和单词的对应,一个用来记录单词是否已经出现过。
与205. Isomorphic Strings几乎是一样的题。
另外,注意文中分词的方法,结合
c++字符串分词
class Solution { public: void splitWords(std::string& s, std::string& delim,std::vector<string >& ret) { size_t last = 0; size_t index=s.find_first_of(delim,last); while (index!=std::string::npos) { ret.push_back(s.substr(last,index-last)); last=index+1; index=s.find_first_of(delim,last); } if (index-last>0) { ret.push_back(s.substr(last,index-last)); } } bool wordPattern(string pattern, string str) { vector<string> strVec; string delim = " "; splitWords(str,delim,strVec); if(strVec.size() != pattern.length()) return false; unordered_map<string,char> str2pattern; unordered_map<char,string> pattern2str; for(int i = 0;i<pattern.length();++i) { if(str2pattern.find(strVec[i]) != str2pattern.end() && str2pattern[strVec[i]] !=pattern[i] || pattern2str.find(pattern[i]) != pattern2str.end() && pattern2str[pattern[i]] !=strVec[i]) return false; str2pattern[strVec[i]] =pattern[i]; pattern2str[pattern[i]] =strVec[i]; } return true; } };
class Solution { public: bool wordPattern(string pattern, string str) { vector<string> wordsVec; map<char, string> hash_table1; map<string, bool> hash_table2; stringstream ss(str); string curStr; while (getline(ss, curStr, ' ')) { wordsVec.push_back(curStr); } if (pattern.length() != wordsVec.size()) { return false; } for (int i = 0; i < pattern.length();++i) { if (hash_table1.find(pattern[i]) == hash_table1.end()) { if (hash_table2[wordsVec[i]] == true) { return false; } else { hash_table1[pattern[i]] = wordsVec[i]; hash_table2[wordsVec[i]] = true; } } else { if (hash_table1[pattern[i]] != wordsVec[i]) { return false; } } } return true; } };
相关文章推荐
- json数据格式
- Android 软键盘遮挡PopupWindow解决办法
- 表单提交的相关注意事项,submit-onclick-事件触发执行过程
- [Android] 建立与使用Library
- 使用loadrunner对https协议(单双向SSL)的web端性能测试 (转)
- cpu topology
- IOS学习之多线程(7)--GCD的基本使用
- 轻松学习jQuery插件EasyUI EasyUI实现树形网络基本操作(2)
- linux服务学习之crond
- TextView高效、友好的用户设计
- CentOS 开机自启动脚本
- 从Java类库看设计模式(3)
- IE11之访问localhost浏览器关闭
- css案例学习之div+a实现菜单
- 特征选择常用算法综述
- excel表格中如何将内容粘贴到筛选后的可见单元格[转]
- FC FCoe Vsan Zone相关
- Android NDK 教程 - NDK环境配置和 Android Studio 中的入门使用
- 从Java类库看设计模式(2)
- 在CentOS上安装Java环境:使用yum安装java