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. SAP ABAP编程 取得用户中文名称

    有时候我们知道SAP当前用户登录的ID,也就是SY-UNAME.能够取得用户中文名称.例如以下: ***取得用户中文名称 DATA: g_sheet_jsr TYPE string.  "用 ...

  2. Centos curl ssl 替换 NSS 为 OpenSSL

    参考:https://www.latoooo.com/xia_zhe_teng/368.htm 我的系统版本是 Centos 7 64位.为了方便,先安装常用的开发环境. yum groupinsta ...

  3. 求一个正实数X的开方

    问题:求一个正实数X的平方根,不能使用sqrt等库函数. 解析:本题要求求一个正实数的平方根,不能使用sqrt等已有的库函数,我们可以做一下考虑: 利用二分法,mid=X/2.0,若mid*mid&g ...

  4. 成为Linux内核高手的四个方法

    首页 最新文章 资讯 程序员 设计 IT技术 创业 在国外 营销 趣文 特别分享 更多 > - Navigation -首页最新文章资讯程序员设计IT技术- Java & Android ...

  5. Eclipse中GitLab的配置和使用入门

    一.Eclipse中配置GitLab的前提条件 1.1:安装Git客户端 去官网https://git-scm.com/downloads下载合适的版本即可,一般开发环境是windows的就下载win ...

  6. Struts2之server端验证

    声明:在我的教程中有些东西,没有提及到.不是我不知道,而是在我个人来看对你们不是太重要的知识点.你们在看课本时有了解到即可.我不会面面俱到的都给你们提及.我写博文的目的是把我这一年的开发经验通过学习s ...

  7. 未能加载文件或程序集“Microsoft.SqlServer.Sqm, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91”或它的某一个依赖项。系统找不到指定的文件。 (SqlMgmt)

    解决方法: Copy this file "Microsoft.SqlServer.Sqm.dll" in the forder "C:/Program Files/Mi ...

  8. [Algorithm] Check for balanced parentheses using stack

    Algorithm or program to check for balanced parentheses in an expression using stack data structure. ...

  9. 使用Nexus管理maven仓库,setting文件理解

    来到新公司对很多陌生的技术一头雾水,以前在工作中没有真正使用过maven,于是强迫自己蛋定下来一个一个的突破,下面是我对maven的setting配置文件的理解,由于是现学的,难免可能会理解偏差,还请 ...

  10. WIN10系统如何取消右下角的通知菜单,通知图标

    鼠标左键单击通知按钮,然后点击所有设置   在通知和操作页面,取消勾选所有的通知   建议选择在任务栏显示哪些图标,然后勾选显示所有图标