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语言开发微信公众平台(三)——被关注回复与关键词回复

    在上一篇文章中,我们实现了文本消息的接收与响应.可以在用户发送任何内容的时候,回复一段固定的文字.本章节中,我们将对上一章节的代码进行适当的完善,同时实现[被关注回复与关键词回复]功能. 一.微信可提 ...

  2. Android -- onMeasure

    onMeasure调用次数 当Activity获取焦点的时候,它就需要绘制布局.Android框架会处理绘制过程,但这个Activity必须提供它布局树的根节点. 绘制过程是从布局的根节点开始的.这个 ...

  3. Apache Mahout:适合所有人的可扩展机器学习框架

    http://www.ibm.com/developerworks/cn/java/j-mahout-scaling/ 在软件的世界中,两年就像是无比漫长的时光.在过去两年中,我们看到了社交媒体的风生 ...

  4. Cognos11中Dashboard和HTML页面的简单集成

    一.需求 之前很多第三方的程序都是通脱URL的形式可以和cognos Report进行集成,在我前几天的博文<Cognos11中通过URL访问report的设置>一篇中也提到了普通repo ...

  5. HTTP认证与https简介

    HTTP请求报头: Authorization HTTP响应报头: WWW-Authenticate  HTTP认证是基于质询/回应(challenge/response)的认证模式. HTTP认证 ...

  6. [Canvas]用透明PNG图在背景上画前景能不遮挡背景

    欲看动态效果请点击下载并用Chrome/Firefox浏览器打开index,html. 图例: 从效果可以明显的看到,五角星边缘和中心都没有对背景遮挡. 代码: <!DOCTYPE html&g ...

  7. VC++中多字节字符集和Unicode之间的互换

    在Visual C++.NET中,默认的字符集是Unicode,这和Windows默认的字符集是一致的,不过在老的VC6.0等工程中,默认的字符集形式是多字节字符集(MBCS:Multi-Byte C ...

  8. WinForm 之 VS2010发布、打包安装程序

    第一步.在vs2010 打开要打包的应用程序解决方案,右键“ 解决方案 ” → “ 添加 ” → “ 新建项目 ” → “ 其他项目类型 ” → “ 安装和部署 ” → “ Visual Studio ...

  9. 微软BI 之SSRS 系列 - 使用分组 Group 属性实现基于父子递归关系的汇总报表

    基于父子关系的递归结构在公司组织结构里比较常见,基本上都是在一张表里实现的自引用关系.在报表中如果要实现这种效果,并且在这个基础上做一些数据的汇总,可以使用到下面提到的方法. 要实现的效果大致如下 - ...

  10. Java IO的应用之实现大文件复制

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5827481.html  用IO进行文件复制,实质就是用FileInputStream链接要复制的文件,按一定规 ...