-
Notifications
You must be signed in to change notification settings - Fork 33
/
add-and-search-word(AC).cpp
108 lines (98 loc) · 2.59 KB
/
add-and-search-word(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
#include <string>
using namespace std;
/**
* Your Trie object will be instantiated and called as such:
* Trie trie;
* trie.insert("lintcode");
* trie.search("lint"); will return false
* trie.startsWith("lint"); will return true
*/
static const int NUM_LETTER = 26;
class TrieNode {
public:
// Initialize your data structure here.
TrieNode() {
child.resize(26, NULL);
isWord = false;
}
void addChild(int index) {
if (index < 0 || index >= NUM_LETTER) {
return;
}
this->child[index] = new TrieNode();
}
TrieNode *getChild(int index) {
if (index < 0 || index >= NUM_LETTER) {
return NULL;
}
return this->child[index];
}
void setIsWord(bool isWord) {
this->isWord = isWord;
}
bool getIsWord() {
return this->isWord;
}
protected:
vector<TrieNode *> child;
bool isWord;
};
class WordDictionary {
public:
WordDictionary() {
root = new TrieNode();
}
// Adds a word into the data structure.
void addWord(string word) {
int wl = word.length();
int i;
TrieNode *ptr = root;
for (i = 0; i < wl; ++i) {
if (ptr->getChild(word[i] - 'a') == NULL) {
ptr->addChild(word[i] - 'a');
}
ptr = ptr->getChild(word[i] - 'a');
}
ptr->setIsWord(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) {
bool suc = false;
DFS(root, word, 0, suc);
return suc;
}
protected:
TrieNode* root;
void DFS(TrieNode *r, const string &s, int idx, bool &suc) {
if (suc) {
return;
}
if (idx == s.length() && r!= NULL) {
if (r->getIsWord()) {
suc = true;
}
return;
}
if (s[idx] == '.') {
int i;
for (i = 0; i < NUM_LETTER; ++i) {
if (r->getChild(i) != NULL) {
DFS(r->getChild(i), s, idx + 1, suc);
}
}
return;
}
while (idx < s.length() && s[idx] != '.') {
r = r->getChild(s[idx++] - 'a');
if (r == NULL) {
return;
}
}
DFS(r, s, idx, suc);
}
};
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");