题目
word-pattern
算法
* 哈希
* 两个哈希
代码
* 哈希
class Solution {
public:
bool wordPattern(string pattern, string str) {
unordered_map<char, string> m;
istringstream in(str);
int i = 0;
for (string word; in >> word; ++i) {
if (m.find(pattern[i]) != m.end()) {
if (m[pattern[i]] != word) return false;
} else {
for (unordered_map<char, string>::iterator it = m.begin(); it != m.end(); ++it) {
if (it->second == word) return false;
}
m[pattern[i]] = word;
}
}
return i == pattern.size();
}
};
* 两个哈希
class Solution {
public:
bool wordPattern(string pattern, string str) {
unordered_map<char, int> m1;
unordered_map<string, int> m2;
istringstream in(str);
int i = 0;
for (string word; in >> word; ++i) {
if (m1.find(pattern[i]) != m1.end() || m2.find(word) != m2.end()) {
if (m1[pattern[i]] != m2[word]) return false;
} else {
m1[pattern[i]] = m2[word] = i + 1;
}
}
return i == pattern.size();
}
};