Easy Java Solution with HashMap in O(n)

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

Log in to reply

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