File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments