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

 class WordDictionary {
private TrieNode root; /** Initialize your data structure here. */
public WordDictionary() {
root = new TrieNode();
} /** Adds a word into the data structure. */
public void addWord(String word) {
TrieNode cur = root;
for (char c: word.toCharArray()) {
if (cur.children[c - 'a'] == null) {
cur.children[c - 'a'] = new TrieNode();
}
cur = cur.children[c - 'a'];
}
cur.isWord = true;
} /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
public boolean search(String word) {
return helper(word, root, 0);
} private boolean helper(String word, TrieNode root, int level) {
if (level == word.length()) {
return root.isWord;
}
char cur = word.charAt(level);
if (cur == '.') {
for (TrieNode child : root.children) {
if (child != null && helper(word, child, level + 1)) {
return true;
}
}
return false;
} else {
return root.children[cur - 'a'] != null && helper(word, root.children[cur - 'a'], level + 1);
}
}
} class TrieNode {
TrieNode[] children;
boolean isWord; public TrieNode() {
children = new TrieNode[26];
isWord = false;
}
} /**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/

[LC] 211. Add and Search Word - Data structure design的更多相关文章

  1. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  2. 【LeetCode】211. Add and Search Word - Data structure design

    Add and Search Word - Data structure design Design a data structure that supports the following two ...

  3. 【刷题-LeetCode】211. Add and Search Word - Data structure design

    Add and Search Word - Data structure design Design a data structure that supports the following two ...

  4. (*medium)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 ...

  5. 211. Add and Search Word - Data structure design

    题目: Design a data structure that supports the following two operations: void addWord(word) bool sear ...

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

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

  7. Java for LeetCode 211 Add and Search Word - Data structure design

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

  8. leetcode@ [211] Add and Search Word - Data structure design

    https://leetcode.com/problems/add-and-search-word-data-structure-design/ 本题是在Trie树进行dfs+backtracking ...

  9. leetcode 211. Add and Search Word - Data structure design Trie树

    题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...

随机推荐

  1. 00java语法基础和课后实践

    一:运行代码,并分析结果 代码1: package reserve; public class Main { public static void main(String[] args) { Size ...

  2. JVM探秘:jmap生成内存堆转储快照

    本系列笔记主要基于<深入理解Java虚拟机:JVM高级特性与最佳实践 第2版>,是这本书的读书笔记. jmap 命令用来生成内存堆转储快照,一般称为heapdump或dump文件. 除了使 ...

  3. 小程序Java多次请求Session不变

    微信小程序每次请求的sessionid是变化的,导致对应后台的session不一致,无法获取之前保存在session中的openid和sessionKey. 为了解决这个问题,需要强制同意每次小程序前 ...

  4. springCloud eureka服务治理集群增加安全认证

    做为SpringCloud Netflix服务套件中的一部分,springCloud eureka基于Netflix Eureka做了二次封装,默认提供WEB管理页面及服务治理. 为了确保在生产环境中 ...

  5. tensorflow deeplabv3 训练自己的数据集

    https://blog.csdn.net/malvas/article/details/90776327

  6. Web API接口

    Web API接口 一.什么是Web API接口 通过网络,规定了前后台信息交互规则的url链接,也就是前后台信息交互的媒介 Web API接口和一般的url链接还是有区别的,Web API接口简单概 ...

  7. Properties类(一)

    Java中读写资源文件最重要的类是Properties,功能大致如下: 1. 读写Properties文件 2. 读写XML文件 3. 不仅可以读写上述两类文件,还可以读写其它格式文件如txt等,只要 ...

  8. 20199324《Linux内核原理与分析》第十二周作业

    格式化字符串漏洞实验 一. 实验描述 格式化字符串漏洞是由像 printf(user_input) 这样的代码引起的,其中 user_input 是用户输入的数据,具有 Set-UID root 权限 ...

  9. StartDT AI Lab | 需求预测引擎如何助力线下零售业降本增效?

    在当下经济明显进入存量博弈的阶段,大到各经济体,小到企业,粗放的增长模式已不适宜持续,以往高增长的时代已经成为过去,亟需通过变革发掘新的增长点.对于竞争激烈的线下零售行业而言,则更需如此. 零售行业一 ...

  10. Caffe Ubuntu14.04 + CUDA 8 (支持GTX1080 1070等Pascal架构显卡)

    1. 前言 本教程使用的系统是Ubuntu 14.04 LTS 64-bit,使用的CUDA版本为8. 理论上本教程支持Pascal架构显卡,如游戏卡GeForce GTX1070,GTX 1080, ...