Easy-understanding Java solution


  • 3
    Y
    public boolean isIsomorphic(String s, String t) {
        if(s.length() != t.length()) return false;
        HashMap<Character, Character> hash = new HashMap<Character, Character>();
        for(int i = 0; i < s.length(); i++){
            if(hash.containsKey(s.charAt(i))){
                if(t.charAt(i) != hash.get(s.charAt(i))) return false;
            }else{
                if(hash.containsValue(t.charAt(i))) return false;
                else hash.put(s.charAt(i), t.charAt(i));
            }
        }
        return true;
    }

  • 0
    M

    containsValue is not O(1) operation. You could use an extra HashSet or HashMap


  • 0
    L

    Use 2 arrays instead


Log in to reply
 

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