-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindAndReplacePattern.cpp
More file actions
32 lines (32 loc) · 930 Bytes
/
findAndReplacePattern.cpp
File metadata and controls
32 lines (32 loc) · 930 Bytes
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) {
int pat[26] = {}, curr = 1, n = pattern.size();
vector<int> code(n, 0);
for (int i = 0; i < n; ++i) {
int idx = pattern[i] - 'a';
if (!pat[idx]) {
pat[idx] = curr++;
}
code[i] = pat[idx];
}
vector<string> ans;
for (auto &w: words) {
curr = 1;
memset(pat, 0, 26 * sizeof(int));
bool flag = true;
for (int i = 0; i < n; ++i) {
int idx = w[i] - 'a';
if (!pat[idx])
pat[idx] = curr++;
if (pat[idx] != code[i]) {
flag = false;
break;
}
}
if (flag)
ans.push_back(w);
}
return ans;
}
};