Skip to content

Commit e793e0a

Browse files
authored
Merge pull request gzc426#180 from zjukk/master
pull request
2 parents 82917bb + 4d81983 commit e793e0a

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

2018.11.30-leetcode890/zjukk.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
C++:
2+
class Solution {
3+
public:
4+
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
5+
vector<string> res;
6+
for (int i = 0; i < words.size(); ++i) {
7+
map<char,char> mp;
8+
map<char,char> mp2;
9+
string word = words[i];
10+
if (pattern.size() != word.size()) continue;
11+
int j = 0;
12+
for (; j < pattern.size(); ++j) {
13+
if (mp.empty() || mp.count(pattern[j]) == 0) {
14+
mp.insert({pattern[j], word[j]});
15+
} else if (mp[pattern[j]] != word[j]){
16+
break;
17+
}
18+
}
19+
if (j != pattern.size()) continue;
20+
int k = 0;
21+
for (; k < pattern.size(); ++k) {
22+
if (mp2.empty() || mp2.count(word[k]) == 0) {
23+
mp2.insert({word[k],pattern[k]});
24+
} else if (mp2[word[k]] != pattern[k]) {
25+
break;
26+
}
27+
}
28+
if (k == pattern.size()) res.push_back(word);
29+
}
30+
return res;
31+
}
32+
};

0 commit comments

Comments
 (0)