-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
35 lines (31 loc) · 913 Bytes
/
Copy pathsolution.cpp
File metadata and controls
35 lines (31 loc) · 913 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
class Solution
{
public:
bool areIsomorphic(string &s1, string &s2)
{
// If lengths are not equal, they cannot be isomorphic
if (s1.length() != s2.length())
return false;
// Mapping arrays (ASCII size)
vector<int> map1(256, -1); // s1 -> s2
vector<int> map2(256, -1); // s2 -> s1
for (int i = 0; i < s1.length(); i++)
{
char c1 = s1[i];
char c2 = s2[i];
// If both characters are not mapped yet
if (map1[c1] == -1 && map2[c2] == -1)
{
map1[c1] = c2;
map2[c2] = c1;
}
// If mapping exists, it must be consistent
else
{
if (map1[c1] != c2 || map2[c2] != c1)
return false;
}
}
return true;
}
};