Using two arrays is enough.


  • 0
    public boolean isIsomorphic(String s, String t) {
            if (s.length() != t.length()) return false;
            int[] map1 = new int[128];
            int[] map2 = new int[128];
            for (int i = 0; i < s.length(); i++) {
                if (map1[s.charAt(i)] == 0 && map2[t.charAt(i)] == 0) {
                    map1[s.charAt(i)] = t.charAt(i);
                    map2[t.charAt(i)] = s.charAt(i);
                } else if (map1[s.charAt(i)] != 0 && map2[t.charAt(i)] != 0) {
                    if (map1[s.charAt(i)] != t.charAt(i) || map2[t.charAt(i)] != s.charAt(i)) return false;
                } else return false;
            }
            return true;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.