Skip to content

Commit

Permalink
docs: add 288
Browse files Browse the repository at this point in the history
  • Loading branch information
Aden-Q committed May 11, 2024
1 parent 982ca14 commit 4e1f0c5
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/288.Unique-Word-Abbreviation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class ValidWordAbbr:

def __init__(self, dictionary: List[str]):
# a map between abbreviation and a set of words of this abbreviation
self.t = defaultdict(set)

for word in dictionary:
abbrev = self.getAbbrev(word)
self.t[abbrev].add(word)

def getAbbrev(self, word: str) -> str:
if len(word) < 3:
return word

return word[0] + str(len(word) - 2) + word[-1]

def isUnique(self, word: str) -> bool:
abbrev = self.getAbbrev(word)
if abbrev not in self.t or (len(self.t[abbrev]) == 1 and word in self.t[abbrev]):
return True

return False


# Your ValidWordAbbr object will be instantiated and called as such:
# obj = ValidWordAbbr(dictionary)
# param_1 = obj.isUnique(word)

0 comments on commit 4e1f0c5

Please sign in to comment.