Unique Word Abbreviation
这里的unique不光说的是这个缩写在字典里面没有,如果字典里面有这个缩写但是只有这一个词也是可以的
class ValidWordAbbr(object):
def __init__(self, dictionary):
"""
:type dictionary: List[str]
"""
self.store = {}
for word in dictionary:
abbr = word
length = len(word)
if length > 2:
abbr = word[0] + `length - 2` + word[-1]
self.store.setdefault(abbr, set([]))
self.store[abbr].add(word)
def isUnique(self, word):
"""
:type word: str
:rtype: bool
"""
abbr = word
length = len(word)
if length > 2:
abbr = word[0] + `length - 2` + word[-1]
if abbr not in self.store:
return True
if word in self.store[abbr] and len(self.store[abbr]) == 1:
return True
return False
# Your ValidWordAbbr object will be instantiated and called as such:
# obj = ValidWordAbbr(dictionary)
# param_1 = obj.isUnique(word)