Update unique_word_abbreviation.py

This commit is contained in:
marina 2023-08-07 17:38:13 -07:00 committed by GitHub
parent b9b5d8e21c
commit fd291fa48b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,17 +14,22 @@ that word and word are the same.
class ValidWordAbbr:
def __init__(self, dictionary: List[str]):
def __init__(self, dictionary):
self.dict = collections.defaultdict(set)
for w in dictionary:
aux_dict[self.get_abr(w)].add(w)
return aux_dict
def get_abr(self, word):
return word[0] + str(len(word[1:-1])) + word[-1] if len(word) != 2 else word
def isUnique(self, word: str) -> bool:
def is_unique(self, word: str) -> bool:
abr = self.get_abr(word)
words = self.dict[abr]
return len(words) == 0 or (len(words) == 1 and word in words)