zoukankan      html  css  js  c++  java
  • 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)
    
  • 相关阅读:
    完成后台管理系统功能(三)查询商品信息列表
    完成后台管理系统功能(二)有关SSM的整合
    实现后台管理系统功能(一)maven工程的建立
    开始‘京西商城’的电商项目(SSM)
    到此,使用struts2+hibernate实现登陆以及学生列表的增删改查 结束 -------------------------------------------------------
    chrome 中 preview 和 response 数据不一致
    单元测试执行过程中忽略出错的单元测试
    使用Maven 构建时跳过单元测试
    使用 AutoIt3 加密解密数据
    SpringBoot 启动流程
  • 原文地址:https://www.cnblogs.com/bernieloveslife/p/10051059.html
Copyright © 2011-2022 走看看