Javascript Solution


  • 0
    C
    var isIsomorphic = function(s, t) {
        if (s.length !== t.length) return false;
        // Each string has to have its own dictionary
        var sDict = new Map();
        var tDict = new Map();
        for (var i = 0; i < s.length; i++ ) {
            var sc = s[i];
            var tc = t[i];
            // If both dictionaries do not have their respective characters mapped yet, 
            // then we should set them
            if (!sDict.has(sc) && !tDict.has(tc)) {
                sDict.set(sc,tc);
                tDict.set(tc,sc);    
            }
    
            // If either char does not match then it is not isomorphic 
            if (sDict.get(sc) !== tc || tDict.get(tc) !== sc) {
                return false;
            }
        }
        return true;
    };
    

Log in to reply
 

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