Easy-understanding Java solution

  • 3
    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(t.charAt(i) != hash.get(s.charAt(i))) return false;
                if(hash.containsValue(t.charAt(i))) return false;
                else hash.put(s.charAt(i), t.charAt(i));
        return true;

  • 0

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

  • 0

    Use 2 arrays instead

Log in to reply

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