Add and Search Word - Data structure design

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.

click to show hint.

You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first.
 
Trie树的递归版。
class TrieNode
{
public:
TrieNode* children[];
bool end;
TrieNode()
{
for(int i = ; i < ; i ++)
children[i] = NULL;
end = false;
}
}; class WordDictionary {
public:
WordDictionary()
{
root = new TrieNode();
} // Adds a word into the data structure.
void addWord(string word) {
TrieNode* cur = root;
int i = ;
while(i < word.size() && cur->children[word[i]-'a'] != NULL)
{
cur = cur->children[word[i]-'a'];
i ++;
}
if(i == word.size())
cur->end = true;
else
{
while(i < word.size())
{
cur->children[word[i]-'a'] = new TrieNode();
cur = cur->children[word[i]-'a'];
i ++;
}
cur->end = true;
}
} // Returns if the word is in the data structure. A word could
// contain the dot character '.' to represent any one letter.
bool search(string word) {
return search(word, root);
} bool search(string word, TrieNode* cur)
{
if(cur == NULL)
return false;
else if(word == "")
return (cur->end == true);
else
{
if(word[] != '.')
{
if(cur->children[word[]-'a'] == NULL)
return false;
else
return search(word.substr(), cur->children[word[]-'a']);
}
else
{
for(int i = ; i < ; i ++)
{
if(search(word.substr(), cur->children[i]))
return true;
}
return false;
}
}
} TrieNode* root;
}; // Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");

【LeetCode】211. Add and Search Word - Data structure design的更多相关文章

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

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

  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 trie]211. Add and Search Word - Data structure design

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

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

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

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

  7. (*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 ...

  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 添加和查找单词-数据结构设计

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

随机推荐

  1. Java Web 生成临时文件并下载(原)

    概述:本文是  java 服务器端生成文件并下载的示例,并不完善,下载之后一般来说还需要删除临时文件. 注意:临时文件存放在 /WEB-INF/tmp 目录下,所以先要把  tmp 目录建起来. pu ...

  2. Java中夏令时带来的Date不一致问题 (转)

    http://www.cnblogs.com/snake-hand/archive/2013/06/10/3131157.html 最近同事W发现使用Java Date创建日期,在不同的机器上执行,得 ...

  3. 深入理解FFM原理与实践

    原文:http://tech.meituan.com/deep-understanding-of-ffm-principles-and-practices.html 深入理解FFM原理与实践 del2 ...

  4. (纪录片)光的故事 BBC Light Fantastic (2004)

    简介: 导演: Jeremy Turner主演: Simon Schaffer / Dimitri Andreas ... Al Hazen / Edmund Dehn ... Priest/Old ...

  5. Try Before Choosing

     Try Before Choosing Erik Doernenburg CREATing An AppliCATion REquiRES MAKing MAny dECiSionS. Some ...

  6. Android Context完全解析,你所不知道的Context的各种细节

    Context相信所有的Android开发人员基本上每天都在接触,因为它太常见了.但是这并不代表Context没有什么东西好讲的,实际上Context有太多小的细节并不被大家所关注,那么今天我们就来学 ...

  7. asp.net 常用于客户端注册的机器信息

    项目需要:根据客户端信息去获取用户登录信息 1.根据客户端信息,并查询数据库是否有匹配.如果没有则重新插入客户端信息: 2.根据客户端的设置提交用户登录信息,用户登录成功后,查询以前是否有过配置信息, ...

  8. Nginx IP 白名单设置

    1:ip.config 192.168.3.15 1;192.168.3.10 1;192.168.0.8 1; 2:nginx.conf #geoIP的白名单 geo $remote_addr $i ...

  9. JAVA的Spring注入机制事例详解

    一.前言 最近使用Spring里面的依赖注入,比如StudentServiceImple2.java代码: package di.service.imple; import com.mengya.sp ...

  10. 測试AtomicInteger与普通int值在多线程下的递增操作

    日期: 2014年6月10日 作者: 铁锚 Java针对多线程下的数值安全计数器设计了一些类,这些类叫做原子类,当中一部分例如以下: java.util.concurrent.atomic.Atomi ...