-
Notifications
You must be signed in to change notification settings - Fork 3
/
2301.cpp
35 lines (34 loc) · 1.21 KB
/
2301.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
class Solution {
public:
bool matchReplacement(string s, string sub, vector<vector<char>>& mappings) {
// create a hash map to check
// if char in s can be transformed from char in sub
// in O(1) time
// hashmap: char in sub -> {chars in s} (use unordered_set as one map to many)
unordered_map<char, unordered_set<char>> hashmap;
for (auto& mapping : mappings) {
hashmap[mapping[0]].insert(mapping[1]);
}
int m = s.size();
int n = sub.size();
for (int i = 0; i <= m - n; ++i) {
// check each chunk of size n equal to sub
bool found = true;
for (int j = 0; j < n; ++j) {
char to = s[i + j];
char from = sub[j];
// if aleady equal, continue
if (from == to) continue;
// if not, check by the hash map
if (hashmap[from].find(to) == hashmap[from].end()) {
found = false;
break;
}
}
// if there is one chunk in s matching the sub
// return true
if (found) return true;
}
return false;
}
};