Leetcode 211. Add and Search Word - Data structure design

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013596119/article/details/85159210

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.

Answer:

Trie Answer:

class WordDictionary(object):

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.next={}
        self.finish=False

    def addWord(self, word):
        """
        Adds a word into the data structure.
        :type word: str
        :rtype: void
        """
        node=self
        for c in word:
            if node.next.has_key(c):
                node=node.next[c]    
            else:
                newnode=WordDictionary()
                node.next[c]=newnode
                node=node.next[c]
        node.finish=True
                
        

    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
        """
        node=self
        i=0
        l=len(word)
        
        while i<l:
            c=word[i]
            if c==".":
                for tmpnode in node.next.values():
                    if tmpnode.search(word[i+1:]):
                        return True
                return False
            else:
                if node.next.has_key(c):
                    node=node.next[c]   
                else:
                    return False
            i+=1
                
        if node.finish:
            return True
        return False
        


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

有点慢

学习bbbarely的算法

from collections import defaultdict
class WordDictionary(object):

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.dict=defaultdict(set)

    def addWord(self, word):
        """
        Adds a word into the data structure.
        :type word: str
        :rtype: void
        """
        w_len=len(word)
        self.dict[w_len].add(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
        """
        w_len=len(word)
        tmpset=self.dict[w_len]
        
        
        if not tmpset:
            return False
        if word in tmpset:
            return True
        
        for i in range(w_len):
            c=word[i]
            if c!=".":
                tmpset={w for w in tmpset if w[i]==c}
        
        if not tmpset:
            return False
        return True

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

根据长度把词存入dict中

猜你喜欢

转载自blog.csdn.net/u013596119/article/details/85159210