My Java Solution Sharing


  • 0
    public boolean isIsomorphic(String s, String t) {
        HashMap<Character, Character> map = new HashMap<>();
        HashSet<Character> set = new HashSet<>();
        char tempS, tempT;
        for(int i=0; i<s.length(); i++) {
            tempS = s.charAt(i);
            tempT = t.charAt(i);
            if(map.containsKey(tempS)) {
                if(map.get(tempS) != tempT) return false;
            } else {
                map.put(tempS, tempT);
                if(!set.add(tempT)) return false;
            }   
        }
        return true;
    }

Log in to reply
 

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