leetcode刷题,总结,记录,备忘 290
来源:互联网 发布:制作网页用什么软件 编辑:程序博客网 时间:2024/06/08 07:01
leetcode290 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 false.
Notes:
You may assume pattern
contains only lowercase letters, and str
contains lowercase letters separated by a single space.
class Solution {public:bool wordPattern(string pattern, string str) {vector<string> vs;while (1){int pos = str.find(" ");if (pos == string::npos){vs.push_back(str);break;}else{vs.push_back(str.substr(0, pos));str = str.substr(pos + 1);}}if (vs.size() != pattern.size()){return false;}map<string, string> ms;map<string, string> ms2;for (int i = 0; i < pattern.size(); ++i){string t;t += pattern[i];if (ms.find(t) == ms.end() && ms2.find(vs[i]) == ms2.end()){ms[t] = vs[i];ms2[vs[i]] = t;}else{if (ms[t] != vs[i] || ms2[vs[i]] != t){return false;}}}return true;}};
0 0
- leetcode刷题,总结,记录,备忘 290
- leetcode刷题。总结,记录,备忘 104
- leetcode刷题。总结,记录,备忘 100
- leetcode刷题。总结,记录,备忘 191
- leetcode刷题。总结,记录,备忘。168
- leetcode刷题, 总结,记录,备忘 171
- leetcode刷题 ,总结,记录,备忘。144
- leetcode刷题,总结,记录, 备忘 141
- leetcode刷题,总结,记录,备忘。116
- leetcode刷题,总结,记录,备忘。96
- leetcode刷题。记录,总结,备忘。217
- leetcode 刷题, 总结,记录,备忘 35
- leetcode刷题,总结,记录,备忘 226
- leetcode刷题,总结,记录,备忘 169
- leetcode刷题,总结,记录,备忘137
- leetcode刷题,总结,记录,备忘83
- leetcode刷题,总结,记录,备忘70
- leetcode刷题,总结,记录,备忘 108
- JAVA学习代码——虚数/复数的计算
- C#的第17节课
- 洛谷 P2668 斗地主
- 安装部署(八) Hive+Sqoop安装部署和使用
- 【CF 311E】
- leetcode刷题,总结,记录,备忘 290
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree(如果这棵树合法有一个条件:对于树上任意一个点,它的祖先到它的距离小于等于它自己的值)
- Android studio Gradle 基础知识
- JAVA学习代码——增强for循环For each
- 《事半功倍系列 javascript》代码
- Javascript中的css样式常用属性
- poj3983 快算24
- HDU-5813-Elegant Construction-贪心
- android中ToolBar设置setNavigationOnClickListener()方法不起作用