Share my easy Java solution


  • 0
    A

    Use two hash table to search the duplicate letter.

    class Solution {
        public boolean isIsomorphic(String s, String t) {
            Map<Character,Integer> map1 = new HashMap<Character,Integer>();
            Map<Character,Integer> map2 = new HashMap<Character,Integer>();
            
            for(int i =0 ;i<s.length();i++){
                if(map1.containsKey(s.charAt(i))  && map2.containsKey(t.charAt(i)) ){
                    if(map1.get(s.charAt(i)) != map2.get(t.charAt(i)) )
                       return false;
                }else if(!map1.containsKey(s.charAt(i))  && !map2.containsKey(t.charAt(i))){
                    map1.put(s.charAt(i) , i );
                    map2.put(t.charAt(i) , i );
                }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.