forked from shruti170901/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Design Add and Search Words Data Structure.cpp
80 lines (75 loc) · 2.12 KB
/
Design Add and Search Words Data Structure.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
// https://leetcode.com/problems/design-add-and-search-words-data-structure/
class WordDictionary {
public:
/** Initialize your data structure here. */
typedef struct node{
bool end;
node* v[26];
} Node;
Node* init(){
Node* ans=new Node;
ans->end=false;
memset(ans->v, NULL, sizeof(ans->v));
return ans;
}
Node* root=init();
WordDictionary() {
}
/** Adds a word into the data structure. */
void addWord(string word) {
//cout<<word<<endl;
Node *t2=root;
int idx=0, n=word.size();
while(idx<n){
if((t2->v)[word[idx]-'a']!=NULL){
//cout<<word[idx]-'a'<<" .. ";
t2=(t2->v)[word[idx]-'a'];
idx++;
//cout<<"Here\n";
}
else break;
}
while(idx<n){
(t2->v)[word[idx]-'a']=init();
t2=(t2->v)[word[idx]-'a'];
idx++;
}
t2->end=true;
return;
}
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
bool sear(string& word, Node* r, int idx){
int i, n=word.size();
Node* temp;
if(word[idx]!='.'){
temp=(r->v)[word[idx]-'a'];
if(temp==NULL) return false;
if(idx==n-1) return temp->end;
return sear(word, temp, idx+1);
}
else{
bool ans=false;
for(i=0;i<26;i++){
if((r->v)[i]!=NULL){
if(idx==n-1){
if((r->v)[i]->end) return true;
}
else{
ans|=sear(word, (r->v)[i], idx+1);
}
}
}
return ans;
}
}
bool search(string word) {
//cout<<word<<endl;
return sear(word, root, 0);
}
};
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary* obj = new WordDictionary();
* obj->addWord(word);
* bool param_2 = obj->search(word);
*/