A Ruby solution


  • 1

    Remember the dictionary words grouped by their abbreviations.

    class ValidWordAbbr
      def initialize(dictionary)
        @words = dictionary.group_by(&method(:abbr))
      end
    
      def is_unique(word)
        words = @words[abbr(word)]
        !words || words == [word]
      end
      
      private
      
      def abbr(word)
        [word[0], word.size, word[-1]]
      end
    end

  • 0
    M

    well, the word itself may appear multiple times in the dictionary, and such ["a","a"],isUnique("a") will be false, So I think words == [word] should be changed as words.uniq == [word]


Log in to reply
 

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