blob: 26ccbfd4134628ccd0a6ffa24663ea7ff8051443 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
#include "leetcode.h"
class Solution {
public:
bool wordPattern(string pattern, string s) {
vector<int> patMap(26, 0);
unordered_map<string, int> strMap;
int i = 0, n = pattern.size();
istringstream ss(s);
string token;
for (string token; ss >> token; ++i) {
if (i == n || patMap[pattern[i] - 'a'] != strMap[token])
return false;
patMap[pattern[i] - 'a'] = strMap[token] = i + 1;
}
return i == n;
}
};
int main() {
Solution obj;
testBool(obj.wordPattern("abba", "dog cat cat dog")); // true
testBool(obj.wordPattern("abba", "dog cat cat fish")); // false
testBool(obj.wordPattern("aaaa", "dog cat cat dog")); // false
}
|