-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02apr.txt
30 lines (25 loc) · 983 Bytes
/
02apr.txt
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
class Solution {
public:
bool isIsomorphic(string s, string t) {
unordered_map<char, char> mapS2T;
unordered_map<char, char> mapT2S;
for (int i = 0; i < s.size(); ++i) {
char charS = s[i];
char charT = t[i];
// Check if there's a mapping for charS in mapS2T and if it maps to the same character in t
if (mapS2T.find(charS) != mapS2T.end()) {
if (mapS2T[charS] != charT) {
return false;
}
} else { // If no mapping exists, check if charT is already mapped to some other character in mapT2S
if (mapT2S.find(charT) != mapT2S.end()) {
return false;
}
// Create new mapping since it's valid
mapS2T[charS] = charT;
mapT2S[charT] = charS;
}
}
return true;
}
};