Shared python easy solution

  • 1
    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
                # 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]
            abbre = word
        return abbre

  • 0

    this solution actually fails the following case:

Log in to reply

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