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.

思路:构建字典树。搜索时,若当前字符为'.',则将字典中当前位置存在的字符都搜索一遍。

 class DictNode
{
public:
DictNode *dict[];
DictNode ()
{
for (int i = ; i < ; i++)
dict[i] = NULL;
}
};
class WordDictionary {
public:
WordDictionary()
{
root = new DictNode();
}
// Adds a word into the data structure.
void addWord(string word) {
DictNode *cur = root;
for (int i = , len = word.size(); i < len; i++)
{
int loc = (int)(word[i] - 'a');
if (cur->dict[loc] == NULL)
cur->dict[loc] = new DictNode();
cur = cur->dict[loc];
}
if (cur->dict[] == NULL)
cur->dict[] = new DictNode();
}
bool help(DictNode *cur, string word)
{
if (cur == NULL) return false;
if (word.size() == )
{
if (cur->dict[] == NULL) return false;
return true;
}
if (word[] != '.')
{
int loc = (int)(word[] - 'a');
return help(cur->dict[loc], (word.size() > ? word.substr() : ""));
}
else
{
for (int i = ; i < ; i++) if (cur->dict[i] != NULL)
{
if (help(cur->dict[i], (word.size() > ? word.substr() : "")))
return true;
}
return false;
}
} // 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 help(root, word);
}
private:
DictNode *root;
}; // Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");

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

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

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

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

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

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

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

  5. LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design

    字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith  ...

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

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

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

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

  9. leetcode@ [211] Add and Search Word - Data structure design

    https://leetcode.com/problems/add-and-search-word-data-structure-design/ 本题是在Trie树进行dfs+backtracking ...

随机推荐

  1. [原]sencha touch之panel和tabpanel

    最近在弄senchatouch的项目,所以边学习边开发,边记录,直接记录下test code如下: Panel: Ext.application({ name:'itKingApp', launch: ...

  2. [转]9个基于Java的搜索引擎框架

    9个基于Java的搜索引擎框架 在这个信息相当繁杂的互联网时代,我们已经学会了如何利用搜索引擎这个强大的利器来找寻目标信息,比如你会在Google上搜索情人节如何讨女朋友欢心,你也会在百度上寻找正规的 ...

  3. 44、gridview实现下拉刷新、上拉加载更多(最简单实现上下拉操作的开源工程!)

    1.工程加入以下两个文件夹:(参考:https://github.com/jingchenUSTC/PullToRefreshAndLoad) (待会我会将demo打包上传) 2.这个demo只有一个 ...

  4. 设计模式学习笔记——java中常用的设计模式

    单例设计模式(Singleton Pattern) 观察者模式(Observer Pattern) 工厂模式(Factory Pattern) 策略模式(Strategy Pattern) 适配器模式 ...

  5. Marketing learning-3

    Part five brand mantra: the elevator speed 1.mental map:Portrays brand associations and responses fo ...

  6. Chrome autocomplete="off"无效

    如果不希望输入框自动填充,可以设置 input 或 textarea 标签的属性 autocomplete="off". 但是有时 Chrome 会忽视 autocomplete ...

  7. 【LeetCode】Search Insert Position(搜索插入位置)

    这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...

  8. Java Socket实战之三 传输对象

    首先需要一个普通的对象类,由于需要序列化这个对象以便在网络上传输,所以实现java.io.Serializable接口就是必不可少的了,入下: public class User implements ...

  9. P1558 色板游戏 (线段树)

    题目链接 Solution 一个简单的 或 线段树.竟然坑了我一个小时... 因为颜色很小,所以把状态压起来. 然后每个节点上的数值代表当前颜色状态. 然后节点合并很简单,直接或起来. 需要注意一下的 ...

  10. nodeJS学习(9)--- nodeJS模块:exports vs module.exports

    模块简介: 通过Node.js的官方API可以看到Node.js本身提供了很多核心模块 http://nodejs.org/api/ 这些核心模块被编译成二进制文件,可以 require('模块名') ...