7 lines of Java, using HashMap, 1 for loop

  • 0
    public class Solution {
    public boolean isIsomorphic(String s, String t) {
        Map M = new HashMap();
        for (int i=0; i<s.length(); i++)
            if (!M.containsKey(s.charAt(i)) && !M.containsValue(t.charAt(i))) 
            else if (!M.containsKey(s.charAt(i)) || !M.get(s.charAt(i)).equals(t.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.