forked from zhuli19901106/leetcode-zhuli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-and-search-word-data-structure-design_1_AC.cpp
108 lines (93 loc) · 2.46 KB
/
add-and-search-word-data-structure-design_1_AC.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// 1AC
#include <vector>
using std::vector;
static const int DICT_SIZE = 26;
typedef struct TrieNode {
TrieNode(char ch = '\0', struct TrieNode *parent = NULL) {
this->is_word = false;
this->ch = ch;
this->child.resize(DICT_SIZE, NULL);
this->parent = parent;
}
void clear() {
int i;
// Be defensive with destructors
int len = child.size();
for (i = 0; i < len; ++i) {
if (child[i] != NULL) {
child[i]->clear();
}
delete child[i];
child[i] = NULL;
}
child.clear();
}
~TrieNode() {
clear();
}
bool is_word;
char ch;
vector<struct TrieNode *> child;
struct TrieNode *parent;
} TrieNode;
class WordDictionary {
public:
/** Initialize your data structure here. */
WordDictionary() {
root = new TrieNode();
}
/** Adds a word into the data structure. */
void addWord(string word) {
int lw = word.size();
if (lw == 0) {
return;
}
int i;
int idx;
TrieNode *p1;
p1 = root;
for (i = 0; i < lw; ++i) {
idx = word[i] - 'a';
if (p1->child[idx] == NULL) {
p1->child[idx] = new TrieNode(word[i], p1);
}
p1 = p1->child[idx];
}
p1->is_word = 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) {
return dfs(0, root, word);
}
~WordDictionary() {
delete root;
root = NULL;
}
private:
TrieNode *root;
bool dfs(int idx, TrieNode *root, string &s) {
while (idx < s.size() && s[idx] != '.') {
root = root->child[s[idx] - 'a'];
if (root == NULL) {
return false;
}
++idx;
}
if (idx == s.size()) {
return root->is_word;
}
int i;
for (i = 0; i < root->child.size(); ++i) {
if (root->child[i] != NULL && dfs(idx + 1, root->child[i], s)) {
return true;
}
}
return false;
}
};
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* bool param_2 = obj.search(word);
*/