[Leetcode]290. Word Pattern
来源:互联网 发布:淘宝村级服务站申请 编辑:程序博客网 时间:2024/04/30 14:11
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) { stringstream ss(str); string s; vector<string> vec; while(ss >> s) vec.push_back(s); if(vec.size() != pattern.size()) return false; map<char, string> m; int i = 0; for(auto c : pattern) { if(m.count(c)) { if(m[c] != vec[i]) return false; } else { for(auto p : m) { if(p.second == vec[i]) return false; } m[c] = vec[i]; } ++i; } 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
- 进程间、线程间通信方式小结
- 关于 enctype="multipart/form-data" 属性问题
- ext4 文件系统概述
- 【牛客网】copy-list-with-random-pointer题解
- 在一台电脑上运行两个tomcat
- [Leetcode]290. Word Pattern
- 挑战 超大背包问题
- 解决打开文件夹,每次都在新的窗口中
- CSS3 border
- python django 数据库查询方法总结
- R语言data.table速查手册
- 何为语法树
- Android Studio官方文档之添加多种密度的矢量图形
- Android开发之WebView的使用