Java - using HashMap


  • 0
    L
    private static boolean isIsomorphic(String s, String t) {
            if (s.length() != t.length()) {
                return false;
            }
            Map<Character, Character> map = new HashMap<>(s.length());
            for (int i = 0; i <= s.length() - 1; i++) {
                char key = s.charAt(i);
                char value = t.charAt(i);
                Character character = map.get(key);
                if (character == null) {
                    if (map.containsValue(value)) {
                        return false;
                    }
                    map.put(key, value);
                } else if (value != character) {
                    return false;
                }
            }
            return true;
        }
    

Log in to reply
 

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