leetcode 211. Add and Search Word - Data structure design

清疚 2022-07-27 13:34 232阅读 0赞

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.

  1. class TrieNode {
  2. public:
  3. // Initialize your data structure here.
  4. bool isword;
  5. TrieNode*nxt[26];
  6. TrieNode() {
  7. for (int i = 0; i < 26; i++)
  8. nxt[i] = NULL;
  9. isword = false;
  10. }
  11. };
  12. class WordDictionary {
  13. public:
  14. WordDictionary() {
  15. root = new TrieNode();
  16. }
  17. ~WordDictionary()
  18. {
  19. TrieNode*node = root;
  20. vector<TrieNode*>que, collect;
  21. que.push_back(root);
  22. while (!que.empty())
  23. {
  24. vector<TrieNode*>newque;
  25. for (int i = 0; i < que.size(); i++)
  26. {
  27. collect.push_back(que[i]);
  28. for (int j = 0; j < 26; j++)
  29. if (que[i]->nxt[j] != NULL)
  30. newque.push_back(que[i]->nxt[j]);
  31. }
  32. que = newque;
  33. }
  34. for (int i = 0; i < collect.size(); i++)
  35. delete collect[i];
  36. }
  37. // Adds a word into the data structure.
  38. void addWord(string word) {
  39. string s = word;
  40. TrieNode*node = root;
  41. for (int i = 0; i < s.length(); i++)
  42. {
  43. if (node->nxt[s[i] - 'a'] == NULL)
  44. {
  45. TrieNode*n = new TrieNode();
  46. node->nxt[s[i] - 'a'] = n;
  47. }
  48. node = node->nxt[s[i] - 'a'];
  49. }
  50. node->isword = true;
  51. }
  52. void do_once(vector<TrieNode*>&candi,string&s)
  53. {
  54. vector<TrieNode*>newcandi;
  55. for (int i = 0; i < candi.size(); i++)
  56. {
  57. if (s.front() == '.')
  58. {
  59. for (int j = 0; j < 26; j++)
  60. if (candi[i]->nxt[j] != NULL)
  61. newcandi.push_back(candi[i]->nxt[j]);
  62. }
  63. else
  64. if (candi[i]->nxt[s.front() - 'a'] != NULL)
  65. newcandi.push_back(candi[i]->nxt[s.front() - 'a']);
  66. }
  67. s.erase(s.begin(), s.begin() + 1);
  68. candi = newcandi;
  69. }
  70. // Returns if the word is in the data structure. A word could
  71. // contain the dot character '.' to represent any one letter.
  72. bool search(string word) {
  73. string s = word;
  74. vector<TrieNode*>candi;
  75. candi.push_back(root);
  76. while (!s.empty() && !candi.empty())
  77. do_once(candi, s);
  78. if (s.empty() && !candi.empty())
  79. {
  80. for (int i = 0; i < candi.size(); i++)
  81. if (candi[i]->isword)
  82. return true;
  83. }
  84. return false;
  85. }
  86. private:
  87. TrieNode* root;
  88. };
  89. // Your WordDictionary object will be instantiated and called as such:
  90. // WordDictionary wordDictionary;
  91. // wordDictionary.addWord("word");
  92. // wordDictionary.search("pattern");

accepted

发表评论

表情:
评论列表 (有 0 条评论,232人围观)

还没有评论,来说两句吧...

相关阅读