My simple Java solution


  • 0
    D
    public boolean isIsomorphic(String s, String t) {
            if (s.length() != t.length()) {
                return false;
            }
            Map<Character, Integer> map1 = new HashMap<>();
            Map<Character, Integer> map2 = new HashMap<>();
            for (int i=0; i<s.length(); ++i) {
                if (!Objects.equals(map1.put(s.charAt(i), i), map2.put(t.charAt(i), i))) {
                    return false;
                }
            }
            return true;
        }
    

  • 0
    D

    @douglas__ said in My simple Java solution:

    if (s.length() != t.length()) {
    return false;
    }

    Sorry, this block could be removed:

    if (s.length() != t.length()) {
                return false;
            }
    

Log in to reply
 

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