LeetCode:Word Pattern
2016-01-12 12:52
351 查看
Word Pattern
Total Accepted: 21299 TotalSubmissions: 77656 Difficulty: Easy
Given a
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.
Subscribe to see which companies asked this question
Hide Tags
Hash Table
Hide Similar Problems
(E) Isomorphic Strings (H)
Word Pattern II
code:
class Solution { public: bool wordPattern(string pattern, string str) { map<char, int> cmap; map<string, int> smap; int n = pattern.size(); istringstream in(str); int i=0; for(string word;in>>word;i++) { if(cmap[pattern[i]] != smap[word]) return false; cmap[pattern[i]] = smap[word] = i+1; } return i==n; } };
相关文章推荐
- 一道关于随机算法的面试题(转)
- JavaScript里的类和继承
- 3、CreateJS介绍-SoundJS
- Centos 7.0安装Nodejs以及npm
- 项目出现.so文件找不到
- js模拟连接a点击事件无效
- canvas圆周动画
- VBA读取word中的内容到Excel中
- 逆向分析objc,所有类的信息都能在动态调试中获取。
- Activiti 工作流得到最后一次批注的时间
- Maven_非法字符: '\ufeff' 解决方案
- Eclipse怎样设置本地的maven
- 常见jquery 获取控件值,Table内控件值
- WARNING: Heavy swapping observed on system in last 5 mins.
- 2、CreateJS介绍-TweenJS
- Linux设备模型(8)_platform设备
- Markdown 语法速查表
- 关于dns解析时间分析
- iOS多线程gcd
- C++遇到的错误解决笔记