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. TX2在Turtlebot测试kobuki

    1.检查TX2开发板上的ROS,输入: $ roscore 如果ROS安装正确显示 started core service [/rosout] 2.输入检测kobuki 命令 ls /dev/kob ...

  2. 下面介绍mysql中模糊查询的四种用法:

    下面介绍mysql中模糊查询的四种用法: 1,%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示. 比如 SELECT * FROM [user] ...

  3. 一维跳棋(BFS)

    一维跳棋是一种在1×(2N+1) 的棋盘上玩的游戏.一共有N个棋子,其中N 个是黑的,N 个是白的.游戏开始前,N 个白棋子被放在一头,N 个黑棋子被放在另一头,中间的格子空着. 在这个游戏里有两种移 ...

  4. 吴裕雄--天生自然 JAVA开发学习:switch case 语句

    public class Test { public static void main(String args[]){ //char grade = args[0].charAt(0); char g ...

  5. centos快速安装mysql

    1. wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 2. rpm -ivh mysql-community-r ...

  6. Python之小作业

    文档如下: # name, age, score tom, 12, 86 Lee, 15, 99 Lucy, 11, 58 Joseph, 19, 56 第一栏为姓名(name),第二栏为年纪(age ...

  7. web标准介绍

    web标准介绍 web标准: w3c:万维网联盟组织,用来制定web标准的机构(组织) web标准:制作网页遵循的规范 web标准规范的分类:结构标准.表现标准.行为标准. 结构:html.表示:cs ...

  8. 协议(Protocol)

    协议(Protocol)的基本概念 协议的声明看起来比较类似于Java中一个类的接口,但是和接口不同的是:协议没有父类也不能定义实例变量. 协议是一种特殊的程序设计结构,用于声明专门被别的类实现的方法 ...

  9. sbt 设置

    修改 sbtopts for shell # zkk -sbt-dir D:/DATA/.sbt -sbt-boot D:/DATA/.sbt/boot -ivy D:/DATA/.ivy2 修改 s ...

  10. Flink(六) —— 配置文件详解

    基础配置 #============================================================================== # Common #===== ...