A different Java solution


  • 0
    N
    private int checked = 0;
    
    public boolean isIsomorphic(String s, String t) {
        if (checked == 2) {
            return true;
        }
        Map<Character, Character> map = new HashMap<>();
        for (int i = 0; i < s.length(); ++i) {
            Character oldVal = map.put(s.charAt(i), t.charAt(i));
            if (oldVal != null && oldVal != t.charAt(i)) {
                return false;
            }
        }
        checked++;
        return isIsomorphic(t, s);
    }

Log in to reply
 

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