Word Pattern
2016-01-20 22:29
260 查看
Given a
Here follow means a full match, such that there is a bijection between a letter in
non-empty word in
Examples:
pattern =
pattern =
pattern =
pattern =
Notes:
You may assume
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) { int m = pattern.length(); int n = str.length(); int i = 0; int p = 0; int q = 0; map<char, string> table1; map<string, char> table2; while (i < m && p < n) { while (p < n && str[p] != ' ') { p++; } int len = p - q; string temp = str.substr(q, len); if (table1.find(pattern[i]) != table1.end()) { if (table1[pattern[i]] != temp) { return false; } } if (table2.find(temp) != table2.end()) { if (table2[temp] != pattern[i]) { return false; } } table1[pattern[i]] = temp; table2[temp] = pattern[i]; p++; q = p; i++; } if (i != m || p != n+1) { return false; } return true; } };
相关文章推荐
- 下拉刷新全屏时,高度的调整
- QT安装包制作
- Unity 编辑器环境下不能正确加载Android Assetbundle 中的 Shader 解决方法
- Matlab编程学习:用内建函数代替for循环
- NSTimer定时器的使用
- CentOs中JDK与tomcat配置
- Boostrap-Glyphicons图标变成中文
- Problem E: 用数字造数字(II)
- Java快速入门【1~10】
- 设计模式的5大设计原则
- 记一次驴唇不对马嘴的DIY之旅(七)
- 使用python+selenium完成qq空间自动登录小程序
- 欢迎使用CSDN-markdown编辑器
- Swift 闭包(closure)
- 如何用angularjs制作一个完整的表格之一__创建简单表格
- C# 模拟登陆并继续访问其他页面
- jsp 入门 cookie session
- LinkedList 模拟队列的结构
- Problem D: 使用指针访问数组元素--程序填空
- Libevent简介和使用