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. Python+Selenium练习篇之6-利用class name定位元素

    有时候,我们在用firepath(不会的请点这里)查看元素的XPath信息,发现没有可以用来定位的id信息,这个时候我们就需要考虑用其他的可用的来定位元素.本文介绍如何通过元素节点中class nam ...

  2. 并发编程——多进程——multiprocessing开启进程的方式及其属性(3)

    开启进程的两种方式——Process 方式一:函数方法 from multiprocessing import Process import time def task(name): print('% ...

  3. java读取文件(更新jdk7及jdk8)

    以字节的方式读取: InputStream inputStream = new FileInputStream(file); int temp = -1; StringBuilder sb = new ...

  4. wcf获取最新版本之后自己的东西没了

    大概意思,点击显示所有文件,然后在那些文件上右击 包括在项目中就行了 图呢.......我当时添加进来的图呢

  5. TortoiseGit保存用户名和密码的方法

    TortoiseGit在提交或者pull时总会提示你输入用户名密码,非常麻烦,那如何解决呢? 1. 对于TortoiseGit 1.8.1.2及其后的版本,右键选择settings ——> Gi ...

  6. cf 843 B Interactive LowerBound [随机化]

    题面: 传送门 思路: 这是一道交互题 比赛的时候我看到了直接跳过了...... 后来后面的题目卡住了就回来看这道题,发现其实比较水 实际上,从整个序列里面随机选1000个数出来询问,然后从里面找出比 ...

  7. c#的字典序

    //Dictionary System.Collections.DictionaryEntry dic=new System.Collections.DictionaryEntry("key ...

  8. php处理ajax

    首先安装wamp,若安装过mysql则终止进程防止冲突,可以访问localhost说明成功.在www目录下新建项目,使用localhost访问. php: <?php //3.获取ajax传过来 ...

  9. 【AtCoder Regular Contest 076 F】Exhausted (贪心)

    Description 机房里有M台电脑排成一排,第i台电脑的坐标是正整数i. 现在有N个OIer进入了机房,每个OIer需要一台电脑来学tui习ji,同时每个OIer对自己电脑所处的坐标范围有一个要 ...

  10. 简析JVM GC的根搜索算法

    根搜索算法的基本思路是通过一系列的“GC Roots”的对象作为起始点,从这些节点开始往下搜索,搜索的走过的路径称为引用链,当一个对象到“GC Roots”没有引用链可达时(也就是用图论的话说就是从G ...