-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathfindAndReplacePattern.cpp
More file actions
52 lines (49 loc) · 1.26 KB
/
findAndReplacePattern.cpp
File metadata and controls
52 lines (49 loc) · 1.26 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
class Solution {
public:
vector<int> returnPattern(string word) {
map<int, char> m;
vector<int> v;
int pt = 0;
for(auto i : word) {
int flag = 0;
for(auto j : m) {
if(j.second == i) {
v.push_back(j.first);
flag = 1;
}
}
if(flag == 0) {
pt++;
v.push_back(pt);
m[pt] = i;
}
}
return v;
}
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
vector<vector<int>> pat;
for(auto i : words) {
pat.push_back(returnPattern(i));
}
vector<int> src = returnPattern(pattern);
vector<string> ret;
for(int i = 0; i < words.size(); i++) {
if(pat[i] == src)
ret.push_back(words[i]);
}
return ret;
}
};
int main()
{
Solution s;
vector<string> words = {"abc","deq","mee","aqq","dkd","ccc"};
string pattern = "abb";
vector<string> ret = s.findAndReplacePattern(words, pattern);
for(auto i : ret)
cout << i << endl;
return 0;
}