【LeetCode】290. Word Pattern
2016-03-26 15:50
225 查看
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.
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.
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; } };
相关文章推荐
- android中permission_group与permisson区别、作用?
- Imageview属性
- GDC2016 [全境封锁],11个种类5个派系的敌人设计思路
- 对java中int与Integer的一些理解
- 梳理caffe代码blob(三)
- PHP设计模式
- WinXP系统提示未安装打印机驱动的故障原因及解决方法
- Airplane mode的三种切换方式
- Node.js之HTPP URL
- PAT (Advanced Level) Practise 1106 Lowest Price in Supply Chain (25)
- HDU1520 Anniversary party(树形dp)
- Poj 2985 树状数组求第k大
- Linux asm系统调用:32位和64位的区别
- Review Board 的使用
- MFC-CStatusBar(状态栏)
- [leetcode] 317. Shortest Distance from All Buildings 解题报告
- Java中的抽象类和接口
- Spring的依赖注入
- 文件上传
- Win8.1系统中的任务管理器怎么打开?win8.1快速启动任务管理器的三种方法