From fd291fa48b4b29cd08cb38cb3a0dc41e5a432f83 Mon Sep 17 00:00:00 2001 From: marina <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Mon, 7 Aug 2023 17:38:13 -0700 Subject: [PATCH] Update unique_word_abbreviation.py --- arrays_and_strings/unique_word_abbreviation.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/arrays_and_strings/unique_word_abbreviation.py b/arrays_and_strings/unique_word_abbreviation.py index e7f097c..6394018 100644 --- a/arrays_and_strings/unique_word_abbreviation.py +++ b/arrays_and_strings/unique_word_abbreviation.py @@ -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)