class MagicDictionary(object):
def __init__(self):
"""
Initialize your data structure here.
"""
def _candiates(self, word):
for i in xrange(len(word)):
yield word[:i] + '*' + word[i + 1:]
def buildDict(self, dict):
"""
Build a dictionary through a list of words
:type dict: List[str]
:rtype: None
"""
self.word = set(dict)
self.near = collections.Counter(can for word in dict
for can in self._candiates(word))
def search(self, word):
"""
Returns if there is any word in the trie that equals to the given word after modifying exactly one character
:type word: str
:rtype: bool
"""
return any(self.near[can] > 1 or
self.near[can] == 1 and word not in self.word
for can in self._candiates(word))
# Your MagicDictionary object will be instantiated and called as such:
# obj = MagicDictionary()
# obj.buildDict(dict)
# param_2 = obj.search(word)