-
Notifications
You must be signed in to change notification settings - Fork 0
/
893.findAndReplacePattern.cpp
55 lines (39 loc) · 1.08 KB
/
893.findAndReplacePattern.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
#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
vector<string> res;
for(auto word :words) {
if(match(word,pattern)){
res.push_back(word);
}
}
return res;
}
bool match(string word,string pattern){
map<char, char> first;
map<char,char> sec;
auto len = word.size();
for( int i= 0; i != len ; i++ ) {
if(!first.count(word[i])) {
first[word[i]]= pattern[i];
}
if(!sec.count(pattern[i])){
sec[pattern[i]] = word[i];
}
if(first[word[i]] != pattern[i] || sec[pattern[i]] != word[i]) {
return false;
}
}
return true;
}
};
int main()
{
cout << "Hello world!" << endl;
return 0;
}