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.

For 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 a-z.

Solution:

public class WordDictionary {
public class TrieNode{
TrieNode[] childs;
boolean hasWord;
public TrieNode(){
childs = new TrieNode[26];
hasWord = false;
}
} TrieNode root; 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.childs[c-'a']==null){
cur.childs[c-'a'] = new TrieNode();
}
cur = cur.childs[c-'a'];
}
cur.hasWord = 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 searchRecur(root,word,0);
} public boolean searchRecur(TrieNode curNode, String word, int start){
if (curNode==null){
return false;
} if (start>=word.length()){
return curNode.hasWord;
} char c = word.charAt(start);
if (c=='.'){
for (TrieNode child : curNode.childs)
if (searchRecur(child,word,start+1)){
return true;
}
return false;
} else {
return searchRecur(curNode.childs[c-'a'],word,start+1);
}
}
} // Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");

LeetCode-Add and Search Word的更多相关文章

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

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

  2. LeetCode——Add and Search Word - Data structure design

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

  3. leetcode Add and Search Word - Data structure design

    我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...

  4. LeetCode Add and Search Word - Data structure design (trie树)

    题意:实现添加单词和查找单词的作用,即实现字典功能. 思路:'.' 可以代表一个任何小写字母,可能是".abc"或者"a.bc"或者"abc.&quo ...

  5. leetcode面试准备:Add and Search Word - Data structure design

    leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...

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

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

  7. 【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 ...

  8. 【刷题-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 ...

  9. [LintCode] Add and Search Word 添加和查找单词

    Design a data structure that supports the following two operations: addWord(word) and search(word) s ...

  10. (Data structure)Implement Trie && Add and Search Word

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...

随机推荐

  1. maven 私服中央库使用阿里云库

    1.admin登录 进入remote repositories management 2.  设置地址

  2. windows下MongoDB的安装,配置与开机自启动

    关于简介不多说百度去吧少年.. MongoDB详细安装: 1.进入官网,点击DOWNLOAD MONGODB,下载所需要的版本.. 我这里把下载的文件放在d\MongoDB文件夹下,点击下载的官方镜像 ...

  3. sql查看所有表大小的方法

    sql查看所有表大小的方法. 代码: declare @id int ) declare @pages int declare @dbname sysname ,) ,) ,) create tabl ...

  4. 输入LPCWSTR类型字符串

    LPCWSTR tmp = L"xxx"; char*转到LPCWSTR LPCSTR(charTmp)

  5. atitit.orm的缺点与orm框架市场占有率,选型attilax总结

    atitit.orm的缺点与orm框架市场占有率,选型attilax总结 1. attilax的orm框架要求 1 2. orm框架市场占有率 2 3. spring jdbc templt 3 4. ...

  6. JAVA多线程之Synchronized、wait、notify实例讲解

    一.Synchronized synchronized中文解释是同步,那么什么是同步呢,解释就是程序中用于控制不同线程间操作发生相对顺序的机制,通俗来讲就是2点,第一要有多线程,第二当多个线程同时竞争 ...

  7. sensor dma alloc failed问题

    打印信息 [CAPTURE][printCapCapbility:93]:D/ driver:sunxi-vfe [CAPTURE][printCapCapbility:94]:D/ card:sun ...

  8. maven 中使用jstl 错误解决

    maven 中使用jstl表达式中出现如上错误.原因: 导入jstl 的jar包,却没有在pom文件中添加jstl相关的jar依赖项. <!--jstl表达式--> <depende ...

  9. StringJDBC更改数据库的两种方式

    方法一jdbcTemplate.update("INSERT INTO USER VALUES(?, ?, ?, ?)", new Object[] {user.getId(), ...

  10. oracle初始操作

    oracle登录 sqlplus  sys/oracle as sysdba 这个登录之后呢 会出现这个: Connected to an idle instance. 这一步是连接上 [oracle ...