Java Solution in Linear Time


  • 0
    E
    public class Solution {
        public boolean isIsomorphic(String s, String t) {
            return doCheck(s,t) && doCheck(t,s) ;
        }
        public boolean doCheck(String s,String t){
            HashMap<Character,Character>map=new HashMap<Character,Character>();
            for(int i=0;i<s.length();i++){
                if(map.containsKey(s.charAt(i))){
                    char a=map.get(s.charAt(i));
                    char b=t.charAt(i);
                    if(a!=b){
                        return false;
                    }
                }
                else{
                    map.put(s.charAt(i),t.charAt(i));
                }
            }
            return true;
        }
    }
    

Log in to reply
 

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