Java bijection solution


  • 0
    D
    public static boolean isIsomorphic(String s, String t) {
        if(s.length() != t.length()) return false;
        Map<Character, Character> mapStoT = new HashMap<>();
        Map<Character, Character> mapTtoS = new HashMap<>();
        char[] chars1 = s.toCharArray();
        char[] chars2 = t.toCharArray();
        int[] compare = new int[chars1.length];
        for (int i = 0; i < chars1.length; i++) {
            char c1 = chars1[i];
            char c2 = chars2[i];
            mapStoT.putIfAbsent(c1, c2);
            mapTtoS.putIfAbsent(c2, c1);
    
            if(c1 != mapTtoS.get(c2) || c2 != mapStoT.get(c1)) return false;
    
        }
        return true;
    
    }

  • 0
    L

    Arrays are better than maps


Log in to reply
 

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