class Solution {
public boolean isIsomorphic(String s, String t) {
if (s == null || t == null) {
return false;
}
char[] sc = s.toCharArray();
char[] tc = t.toCharArray();
int[] map1 = new int[256];
for (int i = 0; i < sc.length; i++) {
if (map1[sc[i]] == 0) {
map1[sc[i]] = tc[i];
}
else {
if (map1[sc[i]] != tc[i]) {
return false;
}
}
}
int[] map2 = new int[256];
for (int j = 0; j < tc.length; j++) {
if (map2[tc[j]] == 0) {
map2[tc[j]] = sc[j];
}
else {
if (map2[tc[j]] != sc[j]) {
return false;
}
}
}
return true;
}
}
- Used char array to achieve hashing
- Notice that char could be automatically converted to integer number in java
- two loops to prevent errors
- Remember to create a new integer array when doing second loop