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. JEECG 简单实例讲解权限控制

    业务背景:某公司要实现一个日志系统,用来了解员工的工作量饱和情况. 需求: 1.角色分为:员工.经理 两种. 2.员工每天在日志系统中填报工作总结,然后经理进行点评. 3.表单内容包含:姓名.日期.工 ...

  2. 0x01 译文:Windows桌面应用Win32开发简介

    本节课将简单介绍下使用C++开发Windows桌面应用的一些基础知识 目录: 准备你的开发环境 Windows 代码规范 操作字符串 什么是一个Window? WinMain:程序的入口点 1. 准备 ...

  3. CXCommon.h工具类

    #ifndef __XCOMMON_H__ #define __XCOMMON_H__ /******************************************************* ...

  4. python内置函数之vars()

    vars([object]) 返回__dict__属性的值.当不传入参数时,和locals()等效.当函数接收一个参数时,参数可以是模块.类.类实例,或者定义了__dict__属性的对象. >& ...

  5. 示例 Demo 工程和 API 参考链接

    Camera Explorer:有关 Windows Phone8 中有关增强 Camera API 的使用.文章链接 Filter Effects:对拍摄的照片或者图片库中的照片应用 Nokia I ...

  6. java学习之实例变量初始化

    实例变量的初始化方法 第一种:通过构造函数进行初始化. 第二种:通过声明实例字段初始化. 第三种:通过对象代码块初始化. 通过构造函数进行初始化方法 通过构造函数进行对象初始化,必须在类中声明一个带参 ...

  7. Missing separate debuginfos, use: debuginfo-install

    环境:CentOS6.2 64位 操作:使用gdb调试C++查询MySQL数据库的程序 原因: 解决办法: 1.  修改文件/etc/yum.repos.d/CentOS-Debuginfo.repo ...

  8. 基于jquery的适合电子商务网站首页的图片滑块

    今天给大家分享一款基于Sequence.js 的图片滑动效果,特别适合电子商务网站或者企业产品展示功能.带有图片缩率图,能够呈现全屏图片浏览效果.结合 CSS3 Transition 实现响应式的滑块 ...

  9. phoenix 入门

    http://phoenix.apache.org/Phoenix-in-15-minutes-or-less.html Blah, blah, blah - I just want to get s ...

  10. 几种在Linux下查询外网IP的办法。

    几种在Linux下查询外网IP的办法.   Curl 纯文本格式输出: curl icanhazip.com curl ifconfig.me curl curlmyip.com curl ip.ap ...