-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddandSearchWord-Datastructuredesign.cpp
More file actions
72 lines (63 loc) · 1.94 KB
/
AddandSearchWord-Datastructuredesign.cpp
File metadata and controls
72 lines (63 loc) · 1.94 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
#include<iostream>
#include<string>
#include<vector>
using namespace std;
struct TrieNode {
bool isEnd = false;
TrieNode* children[26] = {NULL};
};
class WordDictionary {
public:
TrieNode *root;
/** Initialize your data structure here. */
WordDictionary() {
root = new TrieNode();
}
/** Adds a word into the data structure. */
void addWord(string word) {
TrieNode *cur = root;
for(int i=0;i<word.length();i++){
if(cur->children[word[i] - 'a'] == NULL)
cur->children[word[i] - 'a'] = new TrieNode();
cur = cur->children[word[i] - 'a'];
}
cur->isEnd = 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 search(root, word);
}
bool search(TrieNode *cur, string word) {
//cout<<"word: "<<word<<endl;
for(int i=0;i<word.length();i++){
if(word[i] == '.'){
for(int j=0;j<26;j++){
if(cur->children[j] != NULL){
if(search(cur->children[j], word.substr(i+1))) return true;
}
}
return false;
}else{
if(cur->children[word[i] - 'a'] != NULL) cur = cur->children[word[i] - 'a'];
else return false;
}
}
//cout<<"end: "<<cur->isEnd<<endl;
return cur->isEnd;
}
};
int main(){
WordDictionary *dic = new WordDictionary();
dic->addWord("bad");
dic->addWord("dad");
dic->addWord("mad");
dic->search("pad") ? cout<<"true" : cout<<"false";
cout<<endl;
dic->search("bad") ? cout<<"true" : cout<<"false";
cout<<endl;
dic->search(".ad") ? cout<<"true" : cout<<"false";
cout<<endl;
dic->search("b..") ? cout<<"true" : cout<<"false";
cout<<endl;
return 0;
}