-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
30 lines (24 loc) · 836 Bytes
/
Copy pathsolution.java
File metadata and controls
30 lines (24 loc) · 836 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
class Solution {
public boolean areIsomorphic(String s1, String s2) {
if (s1.length() != s2.length()) return false;
int[] map1 = new int[256]; // s1 -> s2
int[] map2 = new int[256]; // s2 -> s1
// Initialize with -1
for (int i = 0; i < 256; i++) {
map1[i] = -1;
map2[i] = -1;
}
for (int i = 0; i < s1.length(); i++) {
char c1 = s1.charAt(i);
char c2 = s2.charAt(i);
if (map1[c1] == -1 && map2[c2] == -1) {
map1[c1] = c2;
map2[c2] = c1;
} else {
if (map1[c1] != c2 || map2[c2] != c1)
return false;
}
}
return true;
}
}