【leetcode】290. Word Pattern
来源:互联网 发布:儿童启蒙英语软件 编辑:程序博客网 时间:2024/06/09 16:49
解题思路:使用两个map结构,注意要防止abba dog dog dog dog 的情形,所以需要两个Map
class Solution {public: bool wordPattern(string pattern, string str) {unordered_map<char,string> map;unordered_map<string,char> map2;vector<string> vec;int j=0;for(int i=0;i<str.size();i++){if(str[i]==' '){string tmp =str.substr(j,i-j);vec.push_back(tmp);j=i+1;}if(i==str.size()-1){string tmp =str.substr(j,i-j+1);vec.push_back(tmp);}}if(pattern.size()!=vec.size())return false;for(int i=0;i<pattern.size();i++){if(map.find(pattern[i])==map.end()&&map2.find(vec[i])==map2.end()){map.insert(make_pair(pattern[i],vec[i]));map2.insert(make_pair(vec[i],pattern[i]));}else if(map[pattern[i]]!=vec[i]||map2[vec[i]]!=pattern[i])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
- just a test
- 微软发布会推Win10新品 Lumia手机与电脑齐更新
- 边缘触控并非唯一 手机新交互方式盘点
- android-contentprovider
- JDBC查询条件中包含中文,无查询结果(编码问题)
- 【leetcode】290. Word Pattern
- #1050 : 树中的最长路(两次BFS)
- 简述Windows XP与Windows 7系统常见漏洞
- chrome浏览器主页被篡改如何解决
- URL入门
- 读完《visual c++网络编程》 唐文超编著后的领悟
- Linux makefile 教程(转载学习)
- URLConnection的一些用法
- IOS中JSON数据的解析