211. Add and Search Word

Design a data structure that supports the following two operations:

void addWord(word)
bool search(word)

search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.

Example:

addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true

Note:
You may assume that all words are consist of lowercase letters a-z.

Accepted
95,401
Submissions
341,383

Solution1:(TLE)

class WordDictionary:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.l = []

    def addWord(self, word):
        """
        Adds a word into the data structure.
        :type word: str
        :rtype: void
        """
        self.l.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
        """
        def judge(a,b):
            if len(a)!=len(b):
                return False
            i = 0
            while i<len(word):
                if a[i]==b[i]:
                    i += 1
                    continue
                if b[i]=='.':
                    i += 1
                    continue
                return False
            return True
        for w in self.l:
            if judge(w,word):
                return True
        return False

# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)

12 / 13 test cases passed.

Solution2:

import collections
class Node:
    def __init__(self):
        self.children = collections.defaultdict(Node)
        self.isword = False
        
class WordDictionary:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.root = Node()

    def addWord(self, word):
        """
        Adds a word into the data structure.
        :type word: str
        :rtype: void
        """
        current = self.root
        for w in word:
            current = current.children[w]
        current.isword = True
        return 

    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
        """
        return self.match(word,0,self.root)
    
    def match(self,word,index,root):
        if root==None:
            return False
        if index== len(word):
            return root.isword
        if word[index] != '.':
            return root != None and self.match(word,index+1,root.children.get(word[index]))
        else:
            for child in root.children.values():
                if self.match(word,index+1,child):
                    return True
        return False

# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)
原文地址:https://www.cnblogs.com/bernieloveslife/p/10051059.html