Skip to content

Latest commit

 

History

History
42 lines (40 loc) · 1.09 KB

File metadata and controls

42 lines (40 loc) · 1.09 KB

Solution1

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;
    }
}

note

  • 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