Design a data structure that supports the following two operations: addWord(word) and 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.
Notice

You may assume that all words are consist of lowercase letters a-z.
Example

addWord("bad")
addWord("dad")
addWord("mad")
search("pad")  // return false
search("bad")  // return true
search(".ad")  // return true
search("b..")  // return true

LeetCode上的原题,请参见我之前的博客Add and Search Word - Data structure design

class WordDictionary {
public:
struct TrieNode {
bool isLeaf;
TrieNode *child[];
}; WordDictionary() {
root = new TrieNode();
} // Adds a word into the data structure.
void addWord(string word) {
TrieNode *p = root;
for (char c : word) {
int i = c - 'a';
if (!p->child[i]) p->child[i] = new TrieNode();
p = p->child[i];
}
p->isLeaf = 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) {
search(word, root, );
} bool search(string &word, TrieNode *p, int i) {
if (i == word.size()) return p->isLeaf;
if (word[i] == '.') {
for (auto a : p->child) {
if (a && search(word, a, i + )) return true;
}
return false;
} else {
return p->child[word[i] - 'a'] && search(word, p->child[word[i] - 'a'], i + );
}
} private:
TrieNode *root;
};

[LintCode] Add and Search Word 添加和查找单词的更多相关文章

  1. 单词的添加与查找 · Add and Search Word

    [抄题]: 设计一个包含下面两个操作的数据结构:addWord(word), search(word) addWord(word)会在数据结构中添加一个单词.而search(word)则支持普通的单词 ...

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

  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. leetcode面试准备:Add and Search Word - Data structure design

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

  6. (Data structure)Implement Trie && Add and Search Word

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...

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

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

随机推荐

  1. memcpy与memmove的区别

    在面试中经常会被问道memcpy与memove有什么区别? 整理如下: 其实主要在考C的关键字:restrict C库中有两个函数可以从一个位置把字节复制到另一个位置.在C99标准下,它们的原型如下: ...

  2. 用rlwrap使sqlplus可以上下翻页

    下载rlwrap-0.30 从光盘上安装readline-devel和readline 安装rlwrap: #tar -zxvf rlwrap-0.30.tar.gz#cd rlwrap-0.30#. ...

  3. 如何学习FPGA?FPGA学习必备的基础知识

    如何学习FPGA?FPGA学习必备的基础知识 时间:2013-08-12 来源:eepw 作者: 关键字:FPGA   基础知识       FPGA已成为现今的技术热点之一,无论学生还是工程师都希望 ...

  4. C# RFID windows 服务 串口方式

    话说RFID以前很火所以整理一下一年前自己处理的RFID程序,放源码. 一开始觉得他是个很神奇的东西. 包含串口通讯和网络通讯. 由于网络通讯设备太贵,所以国内的设备基本上都是在外置一个比较便宜的模块 ...

  5. Build better apps: Windows 10 by 10 development series

    http://blogs.windows.com/buildingapps/2015/08/05/build-better-apps-windows-10-by-10-development-seri ...

  6. 廖雪峰js教程笔记 1

    遍历Array可以采用下标循环,遍历Map和Set就无法使用下标.为了统一集合类型,ES6标准引入了新的iterable类型,Array.Map和Set都属于iterable类型. 具有iterabl ...

  7. Uva 524 Prime Ring

    如果用全排列生成之后,在判断是否是素数环是会超时的,应该用回溯. 回溯的时候  首先要注意 递归边界 ,结束的时候别忘记判断最后一个和第一个元素能否成立  还有要记得vis的使用和递归之后的清理. # ...

  8. SPOJ REPEATS 后缀数组

    题目链接:http://www.spoj.com/problems/REPEATS/en/ 题意:首先定义了一个字符串的重复度.即一个字符串由一个子串重复k次构成.那么最大的k即是该字符串的重复度.现 ...

  9. yii2.0 的数据的 增

    增加数据 /**     * 添加数据  controller 层     */ //引入对应的model类 use app\models\About; //定义对应的方法固定的actionxxxx ...

  10. http://www.roncoo.com/course/view/a09d8badbce04bd380f56034f8e68be0

    http://www.roncoo.com/course/view/a09d8badbce04bd380f56034f8e68be0