题目链接: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. Nmap使用及常见的参数选项

    概要 当Nmap不带选项运行时,该选项概要会被输出,最新的版本在这里http://www.insecure.org/nmap/data/nmap.usage.txt.它有助于人们记住最常用的选项,但不 ...

  2. Charles模拟数据

    安装好Charles 桌面创建json文件

  3. PHP+FLASH大文件断点续传功能分享

    1.使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc) APC实现方法: 安装APC,参照官方文档安装,可以使 ...

  4. Sql Server2008中自定义函数调用存储过程解决方案

    1.开启sql server 2008远程连接 打开sql server配置管理器 配置SSCM,选中左侧的“SQL Server服务”,确保右侧的“SQL Server”以及“SQL Server ...

  5. spring 接口校验参数(自定义注解)

    1. 注解类 import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.l ...

  6. 模板引擎ejs的include方法

    html无法include header.ejs footer.ejs 最后用 user.ejs在首尾include

  7. mysql语句错误

    select * from order where id = 1; 同学问我这句话有什么问题,乍一看真看不出毛病,后来发现order是mysql关键字,这样写是不对的,所以要加一个双引号才会更好一点 ...

  8. VC CString,int,string,char*之间的转换

    CString转string : CString strMfc = "test"; std::string strStr; strStr = strMfc.GetBuffer(); ...

  9. 如何选择EDM电子邮件服务提供商

    选择一家好的EDM电子邮件服务商非常重要,因为这可以让我们的EDM营销事半功倍,同时可以达到更好的营销效果.下面博主为大家介绍一下选择标准. 一.服务好不好. 这点很重要,当然这里的服务包括售前和售后 ...

  10. WPF 绑定数据方式

    ======================================================================== Foreground="{Binding P ...