290. Word Pattern
来源:互联网 发布:it学什么好 编辑:程序博客网 时间:2024/06/03 12:55
/*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.使用两个hash表,其键为相应的字符与word,其值存储相同的数字,当相同的字符在此出现时,则相应位置上的word的键值如果与其字符的键值相同,则格式一样,否则格式不同。*/#include <iostream>#include <vector>#include <unordered_map>#include <string>using namespace std;class Solution {public: bool wordPattern(string pattern, string str) { vector<string> tmp; int i=0; for(int j=0;i<str.size() && j<=str.size();j++)//split str if(str[j]==' ' || j==str.size()) { tmp.push_back(str.substr(i,j-i)); i=j+1; } if(pattern.size()!=tmp.size()) return false; unordered_map<char,int> mp; unordered_map<string,int> ms; i=0; for(;i<pattern.size();i++) { if(i!=0 && mp[pattern[i]]!=ms[tmp[i]]) return false; mp[pattern[i]]=i+1; ms[tmp[i]]=i+1; } return mp[pattern[i-1]]==ms[tmp[i-1]]; }};int main(){ Solution mys; string pattern="aab"; string str="dog dog dog"; cout<<mys.wordPattern(pattern,str)<<endl; return 0;}
阅读全文
0 0
- 【leetcode】290. Word Pattern
- 【leetcode】290. Word Pattern
- 290. Word Pattern
- 290. Word Pattern
- 290. Word Pattern
- 290. Word Pattern
- 290. Word Pattern LeetCode
- 290. Word Pattern
- 290. Word Pattern
- 290. Word Pattern
- 290. Word Pattern
- 290. Word Pattern
- Leetcode 290. Word Pattern
- 290. Word Pattern
- 【LeetCode】290. Word Pattern
- 290. Word Pattern
- LeetCode-290. Word Pattern
- LeetCode *** 290. Word Pattern
- oracle数据库习题1
- Linux C常用头文件及函数
- JAVA 选择结构switch
- 单例模式
- Kafka 和 EMS 消息批量 ack 的实现
- 290. Word Pattern
- Java Scanner next()和nextLine()的区别
- rqnoj 202
- LeetCode 167. Two Sum II
- 机器学习 和 深度学习 的相关概念
- 用Matlab 音量标准化 处理wav文件
- 一点小总结
- css3常用动画大全:translate、scale、opacity、rotate
- 概率期望 LightOJ-1038