Javascript solution | Runtime: 102ms


  • 0
    var isIsomorphic = function(s, t) {
        if (s.length !== t.length) {
            return false;
        }
    
        var hash = {};
        var mappedChars = {};
    
        for (var i = 0; i < s.length; i++) {
            if (!hash[s[i]] && !mappedChars[t[i]]) {
                // Hash entry doesn't exist and the character hasn't been mapped yet
                hash[s[i]] = t[i];
                mappedChars[t[i]] = true;
            } else {
                // Hash entry exists. Now, check if that hash(s[i]) === t[i]
                if (hash[s[i]] !== t[i]) {
                    return false;
                }
            }
        }
    
        return true;
    };
    

Log in to reply
 

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