-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path890.cpp
More file actions
27 lines (27 loc) · 774 Bytes
/
890.cpp
File metadata and controls
27 lines (27 loc) · 774 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
class Solution {
public:
bool isBijective(string& s, string& t) {
vector<int> s2t(26, -1);
vector<int> t2s(26, -1);
int n = s.size();
for (int i = 0; i < n; ++i) {
int si = s[i] - 'a';
int ti = t[i] - 'a';
if (s2t[si] != -1 || t2s[ti] != -1) {
if (t2s[ti] != si || s2t[si] != ti) return false;
}
else {
s2t[si] = ti;
t2s[ti] = si;
}
}
return true;
}
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
vector<string> res;
for (auto& word : words) {
if (isBijective(word, pattern)) res.push_back(word);
}
return res;
}
};