【LeetCode】290. Word Pattern
来源:互联网 发布:文件打开软件 编辑:程序博客网 时间:2024/06/10 02:45
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>vc; map<char,string>m1; map<string,char>m2; if(pattern.length()==0 && str.length()!=0) return false; if(pattern.length()!=0 && str.length()==0) return false; int len1 = pattern.size(); int len2 = str.size(); string word; for(int i =0; i < len2; i++) { if(str[i] != ' ') word += str[i]; else { vc.push_back(word); word = ""; } if(i==len2-1) vc.push_back(word); } if(vc.size() != len1) return false; for(int j =0; j <len1; j++) { map<char,string>::iterator it1; map<string, char>::iterator it2; it1 = m1.find(pattern[j]); it2 = m2.find(vc[j]); if(it1==m1.end() && it2==m2.end()) { m1[pattern[j]] = vc[j]; m2[vc[j]] = pattern[j]; } else if(it1!=m1.end() && it2!=m2.end()) { if(m1[pattern[j]] != vc[j] || m2[vc[j]]!= pattern[j]) return false; } else 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
- MFC-CStatusBar(状态栏)
- Airplane mode的三种切换方式
- SAS中的if过滤语句
- 对java中int与Integer的一些理解
- PAT (Advanced Level) Practise 1107Social Clusters (30)
- 【LeetCode】290. Word Pattern
- redis cmd 使用例子
- android中permission_group与permisson区别、作用?
- Maven deploy配置方法
- poj 1083 Moving Tables
- 华硕笔记本安装windows8.1和ubuntu双系统注意事项
- 动态规划求最长递增子序列
- DB2死锁问题与日常维护
- 关于工资的秘密