Javascript, 100% beat


  • 0
    Y
    var ValidWordAbbr = function(dictionary) {
    this.dic = {};
    dictionary.forEach(function(word) {
        if(!word) return;
        var tmp = transfer(word);
        if(tmp in this.dic) {
            var x = this.dic[tmp];
            if(x) {
                if(x !== word) this.dic[tmp] = false;
            }
        }
        else {
            this.dic[tmp] = word;
        }
    }, this);
    

    };

    ValidWordAbbr.prototype.isUnique = function(word) {
    if(!word) return true;
    var tmp = transfer(word);
    if(tmp in this.dic) {
        var wordSet = this.dic[tmp];
        if(!wordSet) return false;
        else return wordSet === word;
    }
    else return true;
    

    };

    function transfer(str) {
    if(str.length < 3) return str;
    return str.charAt(0)+(str.length-2)+str.charAt(str.length-1);
    

    }


Log in to reply
 

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