【一天一道LeetCode】#290. Word Pattern

# 一天一道LeetCode

## （一）题目

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) {
int lenp=pattern.length();
int lens=str.length();
int i = 0;
int j = 0;
unordered_map<string,char> hash;//两个hash表，双向匹配
unordered_map<char,string> hash2;
while(i<lenp&&j<lens){
string temp;
while(j<lens&&str[j]!=' '){//找出str中以空格分隔的字符串
temp+=str[j++];
}
auto iter = hash.find(temp);
auto iter2 = hash2.find(pattern[i]);
if(iter==hash.end()&&iter2==hash2.end()){//如果都不存在
hash[temp] = pattern[i];
hash2[pattern[i]] = temp;//记录双向匹配关系
}
else{
if(hash[temp]==pattern[i]&&hash2[pattern[i]]==temp) {}
else return false;//匹配不上
}
i++;j++;
}
return i>=lenp?j>=lens?true:false:false;//最后需要判断两个字符串是否匹配完
}
};```

Visual C++ 2010代码完成

Android traceview备选方案

Oracle SQL查询或集群地理数据的函数

MVC 3 Ajax.BeginForm的有效XHTML失败了？

Rails，gemfile中的许多gems会减慢站点的速度吗？

iPhone-backbarbuttonem为零，触摸时不亮显

PHP include当“根正斜杠”位于包含路径之前时，例如：“/folder/file.PHP”

Symfony2路由-路由子域