Leetcode 290. Word Pattern
来源:互联网 发布:股票下跌提醒软件 编辑:程序博客网 时间:2024/06/03 17:47
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.
对字符串进行分词,用map保存和模式串字符的对应关系
class Solution {public: bool wordPattern(string pattern, string str) { str += " "; int j = 0; unordered_map<string, string> mp; for(int i = 0; i < str.size(); i++) { int pos = str.find(" ", i); string tmp = str.substr(i, pos - i); i = pos; if(j == pattern.size()) return false; string key(pattern[j] + "\0"); if(mp.find(key) == mp.end()) { if(mp.find(tmp) == mp.end()) { mp[tmp] = key; mp[key] = tmp; } else return false; } else if(mp[key] != tmp) return false; j++; } if(j != pattern.size()) return false; return true; }};
1 0
- 【leetcode】290. Word Pattern
- 【leetcode】290. Word Pattern
- 290. Word Pattern LeetCode
- Leetcode 290. Word Pattern
- 【LeetCode】290. Word Pattern
- LeetCode-290. Word Pattern
- LeetCode *** 290. Word Pattern
- leetcode 290. Word Pattern
- LeetCode 290. Word Pattern
- LeetCode 290. Word Pattern
- [leetcode] 290. Word Pattern
- 【LeetCode】290. Word Pattern
- LeetCode-290.Word Pattern
- Leetcode 290. Word Pattern
- leetcode:290. Word Pattern
- 290.[Leetcode]Word Pattern
- [Leetcode]290. Word Pattern
- LeetCode 290. Word Pattern
- Android Data Binding Library
- windows下搭建sftp服务端,并使用FileZillaClient测试
- Oracle建表约束格式
- jQuery File Upload踩坑记录
- 线程共享与私有
- Leetcode 290. Word Pattern
- 开关电源EMI设计与整改策略!!!
- Mxnet(4)-fcn训练自己的数据
- JS 跨时区时间解决方案
- Python基础学习笔记之二
- 探索 Android Studio
- hive 分桶表
- Python Unittest 自动化单元测试框架Demo
- Android6.0之AMS如何启动app中篇之Task的管理