Shared python easy solution


  • 1
    A
    def __init__(self, dictionary):
        self.abbreviation = {}
        for k in dictionary:
            abbre = self.abbreviateWord(k)
            if abbre not in self.abbreviation:
                self.abbreviation[abbre] = k # if there's only one unique abbreviation, keep it
            else:
                # if there are duplicated abbreviations, it definitely not unique
                self.abbreviation[abbre] = False
                
    def isUnique(self, word):
        abbre = self.abbreviateWord(word)
        return abbre not in self.abbreviation or self.abbreviation[abbre] == word
    
    def abbreviateWord(self, word):
        if len(word) > 2:
            abbre = word[0] + str(len(word) - 2) + word[-1]
        else:
            abbre = word
        return abbre

  • 0
    K

    this solution actually fails the following case:
    ["a"]
    ["a","a"]


Log in to reply
 

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