My Java Solution with 2 maps


  • 0
    G
    public class Solution {
        public boolean isIsomorphic(String s, String t) {
            Map<Character,Character> map1 = new HashMap<>();
            Map<Character,Character> map2 = new HashMap<>();
            char x,y;
            for(int i=0;i<s.length();i++){
                x = s.charAt(i);
                y = t.charAt(i);
                if(map1.containsKey(x)){
                    if(y != map1.get(x)){
                        return false;
                    }
                } else {
                    map1.put(x,y);
                }
                
                if (map2.containsKey(y)){
                    if(x != map2.get(y)){
                        return false;
                    }
                } else {
                    map2.put(y,x);
                }
            }
            return true;
        }
    }

Log in to reply
 

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