leetcode 211. Add and Search Word

来源:互联网 发布:云豹直播源码 编辑:程序博客网 时间:2024/05/24 20:07
class WordDictionary(object):    """    https://discuss.leetcode.com/topic/29809/python-168ms-beat-100-solution    """    def __init__(self):        """        Initialize your data structure here.        """        self.words = collections.defaultdict(list)    def addWord(self, word):        """        Adds a word into the data structure.        :type word: str        :rtype: void        """        self.words[len(word)].append(word)    def search(self, word):        """        Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.        :type word: str        :rtype: bool        """        for a in self.words[len(word)]:            for i,c in enumerate(word):                if a[i] != c and c != '.':                    break            else:                return True        return False# Your WordDictionary object will be instantiated and called as such:# obj = WordDictionary()# obj.addWord(word)# param_2 = obj.search(word)

原创粉丝点击