-
Notifications
You must be signed in to change notification settings - Fork 1
/
Isomorphic Strings.cpp
50 lines (38 loc) · 1.05 KB
/
Isomorphic Strings.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//Approach-1
class Solution {
public:
bool isIsomorphic(string s, string t) {
vector<int> mp1(256, -1);
vector<int> mp2(256, -1);
int n = s.length();
for(int i = 0; i<n; i++) {
char ch1 = s[i];
char ch2 = t[i];
if((mp1[ch1] != -1 && mp1[ch1] != ch2) ||
(mp2[ch2] != -1 && mp2[ch2] != ch1)
)
return false;
mp1[ch1] = ch2;
mp2[ch2] = ch1;
}
return true;
}
};
//Approach-2 (simplified)
class Solution {
public:
bool isIsomorphic(string s, string t) {
vector<int> mp1(256, -1);
vector<int> mp2(256, -1);
int n = s.length();
for(int i = 0; i<n; i++) {
char ch1 = s[i];
char ch2 = t[i];
if(mp1[ch1] != mp2[ch2])
return false;
mp1[ch1] = i;
mp2[ch2] = i;
}
return true;
}
};