9 line of symmetrical Java, very very easy to understand, just 2 map

  • 0
    public boolean isIsomorphic(String s, String t) {
            Map<Character, Character> s2t = new HashMap<>();
            Map<Character, Character> t2s = new HashMap<>();
            for (int i = 0; i < s.length(); i++){
                s2t.put(s.charAt(i), t.charAt(i));
                t2s.put(t.charAt(i), s.charAt(i));
            for(int i =0; i < s.length(); i++){
                if(s2t.get(s.charAt(i)) != t.charAt(i)) return false;
                if(t2s.get(t.charAt(i)) != s.charAt(i)) return false;
            return true;

Log in to reply

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