simple java solution


  • 0
    public class Solution {
        public boolean isIsomorphic(String s, String t) {
            if (s.length() == 0) return t.length() == 0;
            if (s.length() != t.length()) return false;
            if (s.equals(t)) return true;
            return isIsomorphic(new HashMap(), t, s) && isIsomorphic(new HashMap(), s, t);
        }
    
        private boolean isIsomorphic(Map<Character, Integer> map, String s, String t) {
            int i = 0, j = 0;
            while (i < s.length() && j < t.length()) {
                if (map.containsKey(s.charAt(i))) {
                    int idx = map.get(s.charAt(i));
                    if (t.charAt(idx) != t.charAt(j)) return false;
                } else {
                    map.put(s.charAt(i), j);
                }
                i++;
                j++;
            }
            return true;
        }
    }
    

Log in to reply
 

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