3ms Java Solution with two char[] mappings, beats 99.5%


  • 0
    P
        public boolean isIsomorphic(String s, String t) {
            if (s == null || t == null || s.length() != t.length()) {
                return false;
            }
            char[] sArr = s.toCharArray();
            char[] tArr = t.toCharArray();
            
            char[] sMap = new char[256];
            char[] tMap = new char[256];
            
            for (int i = 0; i < sArr.length; i++) {
                char sChar = sArr[i];
                char tChar = tArr[i];
                if (sMap[sChar] == 0 && tMap[tChar] == 0) {
                    sMap[sChar] = tChar;
                    tMap[tChar] = sChar;
                }
                if (sMap[sChar] != tChar || tMap[tChar] != sChar) {
                    return false;
                }
            }
            return true;
        }
    }

  • 0
    S

    @parminder can you please let me know why 256 has been used???


Log in to reply
 

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