[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-11-23增量更新怎么做?-icode9专业技术文章分享
- 2024-11-23压缩包加密方案有哪些?-icode9专业技术文章分享
- 2024-11-23用shell怎么写一个开机时自动同步远程仓库的代码?-icode9专业技术文章分享
- 2024-11-23webman可以同步自己的仓库吗?-icode9专业技术文章分享
- 2024-11-23在 Webman 中怎么判断是否有某命令进程正在运行?-icode9专业技术文章分享
- 2024-11-23如何重置new Swiper?-icode9专业技术文章分享
- 2024-11-23oss直传有什么好处?-icode9专业技术文章分享
- 2024-11-23如何将oss直传封装成一个组件在其他页面调用时都可以使用?-icode9专业技术文章分享
- 2024-11-23怎么使用laravel 11在代码里获取路由列表?-icode9专业技术文章分享
- 2024-11-22怎么实现ansible playbook 备份代码中命名包含时间戳功能?-icode9专业技术文章分享