[LeetCode] 211. Design Add and Search Words Data Structure_Medium tag: Trie, DFS
2021/6/30 6:21:45
本文主要是介绍[LeetCode] 211. Design Add and Search Words Data Structure_Medium tag: Trie, DFS,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the WordDictionary
class:
WordDictionary()
Initializes the object.void addWord(word)
Addsword
to the data structure, it can be matched later.bool search(word)
Returnstrue
if there is any string in the data structure that matchesword
orfalse
otherwise.word
may contain dots'.'
where dots can be matched with any letter.
Example:
Input ["WordDictionary","addWord","addWord","addWord","search","search","search","search"] [[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]] Output [null,null,null,null,false,true,true,true] Explanation WordDictionary wordDictionary = new WordDictionary(); wordDictionary.addWord("bad"); wordDictionary.addWord("dad"); wordDictionary.addWord("mad"); wordDictionary.search("pad"); // return False wordDictionary.search("bad"); // return True wordDictionary.search(".ad"); // return True wordDictionary.search("b.."); // return True
Constraints:
1 <= word.length <= 500
word
inaddWord
consists lower-case English letters.word
insearch
consist of'.'
or lower-case English letters.- At most
50000
calls will be made toaddWord
andsearch
.
Ideas:因为是word相关,所以考虑用trie来做,addWord就跟传统的Trie做的一样,不停往后加,直到最后那个node使node.isWord = True. 建立一个searchNode的helper function, 因为有‘.’, 可能需要从中间的node下去继续找, check first character, if matches, 继续找,否则return False, 如果是‘.’, 那么将所有的children都走一遍,然后recursive search (word[1:], child),如果有True,那么返回True,将所有child 都走遍没有True,最后返回False
Code:
class TrieNode: def __init__(self): self.isWord = False self.children = dict() class WordDictionary: def __init__(self): """ Initialize your data structure here. """ self.root = TrieNode() def addWord(self, word: str) -> None: node = self.root for c in word: if c not in node.children: node.children[c] = TrieNode() node = node.children[c] node.isWord = True def search(self, word: str) -> bool: return self.searchNode(word, self.root) def searchNode(self, word, node) -> bool: if not word: return node.isWord fir = word[0] if fir == '.': for child in node.children: if self.searchNode(word[1:], node.children[child]): return True return False elif fir in node.children: return self.searchNode(word[1:], node.children[fir]) return False # Your WordDictionary object will be instantiated and called as such: # obj = WordDictionary() # obj.addWord(word) # param_2 = obj.search(word)
这篇关于[LeetCode] 211. Design Add and Search Words Data Structure_Medium tag: Trie, DFS的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-25安卓NDK 是什么?-icode9专业技术文章分享
- 2024-12-25caddy 可以定义日志到 文件吗?-icode9专业技术文章分享
- 2024-12-25wordfence如何设置密码规则?-icode9专业技术文章分享
- 2024-12-25有哪些方法可以实现 DLL 文件路径的管理?-icode9专业技术文章分享
- 2024-12-25错误信息 "At least one element in the source array could not be cast down to the destination array-icode9专业技术文章分享
- 2024-12-25'flutter' 不是内部或外部命令,也不是可运行的程序 或批处理文件。错误信息提示什么意思?-icode9专业技术文章分享
- 2024-12-25flutter项目 as提示Cannot resolve symbol 'embedding'提示什么意思?-icode9专业技术文章分享
- 2024-12-24怎么切换 Git 项目的远程仓库地址?-icode9专业技术文章分享
- 2024-12-24怎么更改 Git 远程仓库的名称?-icode9专业技术文章分享
- 2024-12-24更改 Git 本地分支关联的远程分支是什么命令?-icode9专业技术文章分享