-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_890.cpp
More file actions
42 lines (40 loc) · 918 Bytes
/
Copy pathP_890.cpp
File metadata and controls
42 lines (40 loc) · 918 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
33
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
#define fast_io \
ios::sync_with_stdio(false); \
cin.tie(nullptr);
using namespace std;
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
vector<string>* res = new vector<string>();
int n = pattern.length();
for (string w : words) {
if (match(pattern, n, w)) {
res->push_back(w);
}
}
return *res;
}
bool match(string& pattern, int n, string& w) {
char mapL[26];
char mapR[26];
memset(mapL, '*', sizeof mapL);
memset(mapR, '*', sizeof mapR);
for (int i = 0; i < n; i++) {
int l = w[i] - 'a';
int r = pattern[i] - 'a';
if (mapL[l] != '*') {
if (mapL[l] != pattern[i]) {
return false;
}
} else {
mapL[l] = pattern[i];
}
if (mapR[r] != '*') {
if (mapR[r] != w[i]) {
return false;
}
} else {
mapR[r] = w[i];
}
}
return true;
}