LeetCode 290. Word Pattern
来源:互联网 发布:安卓软件商店 编辑:程序博客网 时间:2024/06/08 12:34
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.
C++没有split真不方便,去网上找了分割字符串的方法,第一次知道strtok()函数。
class Solution {public: vector<string> split(string str){ char* cstr; cstr = new char[str.size() + 1]; strcpy(cstr, str.c_str()); vector<string> res; char* p; p = strtok(cstr, " "); while(p){ res.push_back(p); p = strtok(NULL, " "); } return res; } bool wordPattern(string pattern, string str) { vector<string> pat = split(str); if(pattern.size() != pat.size()) return false; map<char, string> m1; map<string, char> m2; map<char, string>::iterator it1; map<string, char>::iterator it2; int i, len; len = pattern.size(); for(i = 0; i < len; i ++){ it1 = m1.find(pattern[i]); if(it1 == m1.end()){ m1[pattern[i]] = pat[i]; }else{ if(m1[pattern[i]] != pat[i]) return false; } it2 = m2.find(pat[i]); if(it2 == m2.end()){ m2[pat[i]] = pattern[i]; }else{ if(m2[pat[i]] != pattern[i]) return false; } } return true; }};
0 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
- ovs + dpdk版本说明
- UE4设置Visual Studio
- iOS开发-生命周期
- Docker上关于出现无法识别host的问题解决方案
- 每日一linux命令(38)-------iostat命令
- LeetCode 290. Word Pattern
- ubuntu下如何自动启动U盘脚本程序(autorun)
- ANGULAR2 深入学习路由
- ionic,angularjs,requirejs 结合开发 web
- ZEND STUDIO 设置RUA AS WEB 方式
- 图片缓存原理glide与picasso的区别
- C语言找一组数中单独出现的数字
- 【UVa】10566 - Crossed Ladders(二分 & 数学)
- python学习——编写ORM