JavaScript using object for screening


  • 0
    E
    var isIsomorphic = function(s, t) {
        var avaliable = {};
        var replace = {};
        for(let i = 0; i < t.length; i++){
            if(replace[t[i]] !== s[i] && replace[t[i]] !== undefined) return false;
            if(avaliable[s[i]] !== undefined && replace[t[i]] === undefined) return false;
            replace[t[i]] = s[i];
            avaliable[s[i]] = 1;
        }
        return true;
    };
    

    "avaliable" stores the target letter that has been mapped to, "replace" stores the source letter that has mapped to some character.


Log in to reply
 

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