Add and Search Word - Data structure design

Design a data structure that supports the following two operations:

  1. void addWord(word)
  2. 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:

  1. addWord("bad")
  2. addWord("dad")
  3. addWord("mad")
  4. search("pad") -> false
  5. search("bad") -> true
  6. search(".ad") -> true
  7. 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树的递归版。
  1. class TrieNode
  2. {
  3. public:
  4. TrieNode* children[];
  5. bool end;
  6. TrieNode()
  7. {
  8. for(int i = ; i < ; i ++)
  9. children[i] = NULL;
  10. end = false;
  11. }
  12. };
  13.  
  14. class WordDictionary {
  15. public:
  16. WordDictionary()
  17. {
  18. root = new TrieNode();
  19. }
  20.  
  21. // Adds a word into the data structure.
  22. void addWord(string word) {
  23. TrieNode* cur = root;
  24. int i = ;
  25. while(i < word.size() && cur->children[word[i]-'a'] != NULL)
  26. {
  27. cur = cur->children[word[i]-'a'];
  28. i ++;
  29. }
  30. if(i == word.size())
  31. cur->end = true;
  32. else
  33. {
  34. while(i < word.size())
  35. {
  36. cur->children[word[i]-'a'] = new TrieNode();
  37. cur = cur->children[word[i]-'a'];
  38. i ++;
  39. }
  40. cur->end = true;
  41. }
  42. }
  43.  
  44. // Returns if the word is in the data structure. A word could
  45. // contain the dot character '.' to represent any one letter.
  46. bool search(string word) {
  47. return search(word, root);
  48. }
  49.  
  50. bool search(string word, TrieNode* cur)
  51. {
  52. if(cur == NULL)
  53. return false;
  54. else if(word == "")
  55. return (cur->end == true);
  56. else
  57. {
  58. if(word[] != '.')
  59. {
  60. if(cur->children[word[]-'a'] == NULL)
  61. return false;
  62. else
  63. return search(word.substr(), cur->children[word[]-'a']);
  64. }
  65. else
  66. {
  67. for(int i = ; i < ; i ++)
  68. {
  69. if(search(word.substr(), cur->children[i]))
  70. return true;
  71. }
  72. return false;
  73. }
  74. }
  75. }
  76.  
  77. TrieNode* root;
  78. };
  79.  
  80. // Your WordDictionary object will be instantiated and called as such:
  81. // WordDictionary wordDictionary;
  82. // wordDictionary.addWord("word");
  83. // 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. THINKPHP 解决模块不存在时出现空页面的问题

    遇到的问题: 最近使用THINKCMF开发了一个企业网站,因为之前客户的域名变更过,然后就发现当某个模块不存在的时候就出现了空页面 在 THINKPHP论坛 上有人说在项目里添加一个EmptyActi ...

  2. (转)mobile cpu上禁用alpha test的相关总结

    转自:http://www.cnblogs.com/TracePlus/p/4037165.html 因为,每家芯片的特性不同,根据向framebuffer写法的不同,分为tile-based的mob ...

  3. 在linux 中wget 无法解析主机

    vim /etc/resolv.cof 在里面加入节点 nameserver 8.8.8.8 / nameserver 8.8.4.4 即可 失败时: 成功时:

  4. OpenGL ES 3.0之Uniform详解

    Uniform是变量类型的一种修饰符,是OpenGL ES  中被着色器中的常量值,使用存储各种着色器需要的数据,例如:转换矩阵.光照参数或者颜色. uniform 的空间被顶点着色器和片段着色器分享 ...

  5. Laravel的目录结构分析

    根目录结构/app/bootstrap/public/vendorartisancomposer.jsonserver.php 1./app整个Laravel 目录中最需要我们注意的地方,包含设置(c ...

  6. 使用jstl报错:Can not find the tag library descriptor for “http://java.sun.com/jstl/core”

    使用jstl报错:Can not find the tag library descriptor for “http://java.sun.com/jstl/core” 出现这个错误的原因是项目中没有 ...

  7. SpeechLib 应用

    //引用组件:Interop.SpeechLib.dll //导入空间:SpeechLib //引用组件:Interop.SpeechLib.dll//导入空间:SpeechLib //1.SpVoi ...

  8. 如何设置Apache中的最大连接数

    Apache的主要工作模式有两种:prefork和worker 一.两种模式 prefork模式(缺省模式) prefork是Unix平台上的默认(缺省)MPM,使用多个子进程,每个子进程只有一个线程 ...

  9. 推荐五星级C语言学习网站

    www.cprogrammingexpert.com (此网站,配合了大量动画,每一行代码,配合一副图片) 下面截取了部分的gif动画,大家可以认真看看, 相信作者花了很多心血,去制作这些动画. sc ...

  10. 转:Creating a Nested ESXi 5 Environment

    http://tsmith.co/2011/creating-a-nested-esxi-5-environment/ http://tsmith.co/2012/vsphere-5-1-lab-ne ...