Unique Word Abbreviation
简单的模拟题,记住题目要求里奇怪的判断条件就好
class ValidWordAbbr(object):
def __init__(self, dictionary):
"""
:type dictionary: List[str]
"""
self.store = collections.defaultdict(list)
for word in dictionary:
if len(word) <= 2:
self.store[word].append(word)
continue
self.store[word[0] + `len(word) - 2` + word[-1]].append(word)
def isUnique(self, word):
"""
:type word: str
:rtype: bool
"""
if len(word) <= 2:
return True
abbre = word[0] + `len(word) - 2` + word[-1]
if abbre not in self.store:
return True
elif len(self.store[abbre]) == 1 and self.store[abbre][0] == word:
return True
return False
# Your ValidWordAbbr object will be instantiated and called as such:
# obj = ValidWordAbbr(dictionary)
# param_1 = obj.isUnique(word)
这里如果用set会更加快