My Javascript solution


  • 0
    Z
    var ValidWordAbbr = function(dictionary) {
      this.dict = dictionary;
      var hash = {};
    
      dictionary.forEach(function (d) {
        var len = d.length;
        var abbr = d;
    
        if (len > 2) {
          abbr = d.charAt(0) + (len - 2) + d.charAt(len - 1);
        }
    
        if (hash[abbr]) {
          if (!~hash[abbr].indexOf(d)) {
            hash[abbr].push(d);
          }
        } else {
          hash[abbr] = [d];
        }
      });
    
      this.hash = hash;
    };
    
    /**
     * @param {string} word
     * @return {bool}
     */
    ValidWordAbbr.prototype.isUnique = function(word) {
      var abbr = word;
      var len = word.length;
      if (len > 2) {
        abbr = word.charAt(0) + (len - 2) + word.charAt(len - 1);
      }
      var abbrs = this.hash[abbr];
    
      return !abbrs || (abbrs.length === 1 && abbrs[0] === word);
    };

  • 0
    M

    fails for
    input: ["a","a"], isUnique("a")
    output: [false]
    expected: [true]


  • 0
    Z

    thanks, @muratozgul. Seems they have updated test case for this edge case.
    I have updated the solution


Log in to reply
 

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