Isomorphic Strings

hash table and map to record whether the char in s has a isomorphic char and char in t has been mapped.

public boolean isIsomorphic(String s, String t) {
        Map<Character, Character> map = new HashMap();
        boolean[] mapped = new boolean[256];
        char[] sl = s.toCharArray(), tl = t.toCharArray();

        if(sl.length != tl.length) return false;

        for(int i = 0 ; i < sl.length ; i++){
            if(map.get(sl[i]) == null){
                if(mapped[tl[i]]) return false;
                map.put(sl[i], tl[i]);
                mapped[tl[i]] = true;
            }else{
                if(map.get(sl[i]) != tl[i]) return false;
            }
        }

        return true;
    }

results matching ""

    No results matching ""