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. 从JSON数据中取出相关数据

    参考: http://www.cnblogs.com/shuilangyizu/p/6019561.html JSON数据如下: { "total": 1, "rows& ...

  2. mysql group replication 安装&配置详解

    一.原起: 之前也有写过mysql-group-replication (mgr) 相关的文章.那时也没有什么特别的动力要写好它.主要是因为在 mysql-5.7.20 之前的版本的mgr都有着各种各 ...

  3. unity, Rigidbody.constraints

    一,同时施加多个限制: 用按位或(bitwise OR)实现,例如: GetComponent<Rigidbody>().constraints=RigidbodyConstraints. ...

  4. Linux 用 shell 脚本 批量 导入 csv 文件 到 mysql 数据库

    前提: 每个csv文件第一行为字段名 创建的数据库字段名同csv 文件的字段名 1. 批量导入 多个 csv 文件 for file in ./*.csv;do mv $file tablename. ...

  5. java开发常用到的jar包总结

    commons-io.jar: FileUtils 读取文件所有行 File file = new File("c:\\123.txt"); List<String> ...

  6. Openstack 03 - Nova Compute

    1.前言 非常早之前就開始着手写Openstack 系列的博客了,在写了总体架构和Keystone之后,准备写Nova,可是每次写到一半,自己心里就认为不踏实,由于似乎我并没有真正理解Nova,或者说 ...

  7. AESDK AE中层类型的3种取得方式

    有一部分属于类型标志,比如调节层,空对象层.用mSuites->LayerSuite7()->AEGP_GetLayerFlags去取 而灯光,文字这些信息,直接取类型即可 mSuites ...

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

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

  9. atitit.Atitit. Gui控件and面板-----服务端控件 java struts的实现最佳实践

    atitit.Atitit.  Gui控件and面板-----服务端控件 java struts的实现最佳实践 1. 服务器控件的类别 1 1.1. 数据控件:该类控件可细分为两种类型:数据源控件和数 ...

  10. 利用GitHub Pages和Hexo搭建个人博客

    本文首发地址: 非生异也 本项目源码托管在GitHub上 Why 阮一峰曾经说过:喜欢写Blog的人,会经历3个阶段. 第一阶段,刚接触Blog,觉得很新鲜,试着选择一个免费空间来写. 第二阶段,发现 ...