题目链接:https://leetcode-cn.com/problems/add-and-search-word-data-structure-design/

题目描述:

设计一个支持以下两种操作的数据结构:

void addWord(word)
bool search(word)

search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z 。 . 可以表示任何一个字母。

示例:

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

说明:

你可以假设所有单词都是由小写字母 a-z 组成的。

思路:

这道题就是使用 前缀树(字典树)

先把前缀树的数据结构练习一下208. 实现 Trie (前缀树) | 题解链接

相关题型:

212. 单词搜索 II

421. 数组中两个数的最大异或值

代码:

class WordDictionary:

    def __init__(self):
"""
Initialize your data structure here.
"""
from collections import defaultdict
self.lookup = {} def addWord(self, word: str) -> None:
"""
Adds a word into the data structure.
"""
tree = self.lookup
for a in word:
tree = tree.setdefault(a, {})
tree["#"] = {} def search(self, word: str) -> bool:
"""
Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.
""" def helper(word, tree):
if not word:
if "#" in tree:
return True
return False
if word[0] == ".":
for t in tree:
if helper(word[1:], tree[t]):
return True
elif word[0] in tree:
if helper(word[1:], tree[word[0]]):
return True
return False
return helper(word, self.lookup)

[LeetCode] 211. 添加与搜索单词 - 数据结构设计的更多相关文章

  1. Java实现 LeetCode 211 添加与搜索单词 - 数据结构设计

    211. 添加与搜索单词 - 数据结构设计 设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则 ...

  2. leetcode 211. 添加与搜索单词 - 数据结构设计 解题报告

    设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a- ...

  3. Leetcode 211.添加与搜索单词

    添加与搜索单词 设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字 ...

  4. 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...

  5. 211 Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    设计一个支持以下两个操作的数据结构:void addWord(word)bool search(word)search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z . ...

  6. [Swift]LeetCode211. 添加与搜索单词 - 数据结构设计 | Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  7. Leetcode211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a- ...

  8. [LeetCode] 211. Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  9. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

随机推荐

  1. 实验 5 Spark SQL 编程初级实践

    实验 5  Spark SQL 编程初级实践    参考厦门大学林子雨 1. Spark SQL 基本操作 将下列 json 数据复制到你的 ubuntu 系统/usr/local/spark 下,并 ...

  2. Chrome开发者工具面板 F12 调试大全 记录

    面板上包含了Elements面板.Console面板.Sources面板.Network面板.Timeline面板.Profiles面板.Application面板.Security面板.Audits ...

  3. python3---99乘法表

    参考: http://www.cnblogs.com/suiy-160428/p/5594389.html #!/usr/bin/env pythonfor i in range(1,10): for ...

  4. python中的类,对象,实例,继承,多态

    ------------恢复内容开始------------ 类 (通俗来讲是 属性和方法的集合) 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法. 对象,即为类 ...

  5. 导出csv文件(使用a标签)

    https://blog.csdn.net/oscar999/article/details/16342699   productsCSV(e) { const { download } = this ...

  6. VO(值对象) 与PO (持久对象)

    VO ,值对象 (Value Object) , PO ,持久对象 (Persisent Object),它们是由一组属性和属性的 get 和 set 方法组成.从结构上看,它们并没有什么不同的地方. ...

  7. nginx展示文件目录

    1. 如何让nginx显示文件夹目录 vi /etc/nginx/conf.d/default.conf 添加如下内容: location / { root /data/www/file //指定实际 ...

  8. Java虚拟机之JVM调节参数

    -XX:+PrintGC 使用这个参数,虚拟机启动后,每次GC就会打印日志. -XX:+UseSerialGC 使用串行垃圾回收器. -XX:+PrintGCDetails 打印详细信息.包括各个区的 ...

  9. final修饰的类,其属性和方法默认是被final修饰的吗?

    在论坛上,看到一个问题,当然,各位聪明的客官想必已经知道问题是什么了,嘿嘿,没错就是文章的标题:final修饰的类,其属性和方法默认是被final修饰的吗? 老实说,刚开始看到这个问题的时候,有点懵. ...

  10. 自动轮播切换tab

    一个项目中需要用到类似的功能,自己手写一个轮播切换,不足之处见谅.代码如下 <!DOCTYPE html> <html lang="en"> <hea ...