forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
290.cpp
55 lines (45 loc) · 1.45 KB
/
290.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <cassert>
using namespace std;
class Solution {
public:
bool wordPattern(string pattern, string str) {
vector<string> words;
map<string, char> word2pattern;
map<char, string> pattern2word;
int i, prev = 0;
for (i = 0; i <= str.length(); i++) {
if (str[i] == ' ' || str[i] == '\0') {
words.push_back(str.substr(prev, i - prev));
prev = i + 1;
}
}
if (pattern.length() != words.size()) return false;
for (i = 0; i < words.size(); i++) {
if (word2pattern.find(words[i]) == word2pattern.end() &&
pattern2word.find(pattern[i]) == pattern2word.end()) {
word2pattern[words[i]] = pattern[i];
pattern2word[pattern[i]] = words[i];
}
else {
if (word2pattern[words[i]] != pattern[i] ||
pattern2word[pattern[i]] != words[i]) {
return false;
}
}
}
return true;
}
};
int main() {
Solution s;
assert(s.wordPattern("abba", "dog cat cat dog") == true);
assert(s.wordPattern("abba", "dog cat cat fish") == false);
assert(s.wordPattern("aaaa", "dog cat cat dog") == false);
assert(s.wordPattern("abba", "dog dog dog dog") == false);
printf("all tests passed!\n");
return 0;
}