My concise and easy understand solution


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

Log in to reply
 

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