-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNo211_AddAndSearchWord-DataStructureDesign.cpp
More file actions
106 lines (94 loc) · 2.86 KB
/
No211_AddAndSearchWord-DataStructureDesign.cpp
File metadata and controls
106 lines (94 loc) · 2.86 KB
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
// 211. Add and Search Word - Data structure design
// Design a data structure that supports the following two operations:
// void addWord(word)
// 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:
// addWord("bad")
// addWord("dad")
// addWord("mad")
// search("pad") -> false
// search("bad") -> true
// search(".ad") -> true
// 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.
const int SIZE = 26;
class TrieNode{
public:
TrieNode *children[SIZE];
bool isLeaf;
TrieNode(){
isLeaf = false;
for(int i=0; i<SIZE; i++){
children[i] = nullptr;
}
}
};
class WordDictionary {
public:
/** Initialize your data structure here. */
WordDictionary() {
root = new TrieNode();
}
/** Adds a word into the data structure. */
void addWord(string word) {
TrieNode *pCrawl = root;
int level = 0;
int len = word.size();
int index = 0;
for(level=0; level<len; level++){
index = word[level] - 'a';
if(!pCrawl->children[index]){
pCrawl->children[index] = new TrieNode();
}
pCrawl = pCrawl->children[index];
}
// Sign for ending
pCrawl->isLeaf = 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) {
TrieNode *pCrawl = root;
//bool flag = true;
bool find_flag = false;
search_for_reg(pCrawl, find_flag, word, 0);
if(find_flag){
return true;
}
return false;
}
private:
TrieNode *root;
void search_for_reg(TrieNode* node, bool &find_flag, string word, int level){
if(!node || level > word.size() || find_flag){
return;
}
if(level == word.size()){
if(node->isLeaf)
find_flag = true;
return;
}
if(word[level]!='.'){
int index = word[level] - 'a';
if(!node->children[index]){
//flag = false;
return;
}
search_for_reg(node->children[index], find_flag, word, level+1);
}
else{
for(int i=0; i<SIZE; i++){
search_for_reg(node->children[i], find_flag, word, level+1);
}
}
}
};
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* bool param_2 = obj.search(word);
*/