-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path890.cpp
More file actions
32 lines (32 loc) · 1.03 KB
/
Copy path890.cpp
File metadata and controls
32 lines (32 loc) · 1.03 KB
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
class Solution {
public:
vector<string> findAndReplacePattern(vector<string> &words, string pattern) {
vector<string> ret;
int size = pattern.size();
for (string word: words) {
int wordSize = word.size();
if (size != wordSize)
continue;
map<char, char> p;
map<char, char> q;
int i = 0;
for (i = 0; i < size; ++i) {
if (p.find(pattern[i]) == p.end()) {
p.insert(map<char, char>::value_type(pattern[i], word[i]));
} else {
if (p[pattern[i]] != word[i])
break;
}
if (q.find(word[i]) == q.end()) {
q.insert(map<char, char>::value_type(word[i], pattern[i]));
} else {
if (q[word[i]] != pattern[i])
break;
}
}
if (i == size)
ret.push_back(word);
}
return ret;
}
};