forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay-05-Add and Search Word - Data structure design.cpp
100 lines (73 loc) · 2.46 KB
/
Day-05-Add and Search Word - Data structure design.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
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.
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.
Hide Hint #1
You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first.
class WordDictionary {
public:
struct TrieNode {
bool ew; // end of word
struct TrieNode* child[26]; // a-z
};
struct TrieNode* root; // define root
struct TrieNode* createNode(){
struct TrieNode* newNode=new TrieNode;
newNode->ew=false; // not end of word
for(int i=0;i<26;i++)
newNode->child[i]=NULL;
return newNode;
}
void insert(struct TrieNode* root, string word){
int n=word.length();
struct TrieNode* curr=root;
for(int i=0;i<n;i++){
int index=word[i]-'a';
if(curr->child[index]==NULL)
curr->child[index]=createNode();
curr=curr->child[index];
}
curr->ew=true; // mark end of word
}
bool searchUtil(struct TrieNode* node, int i, string word){
if(node==NULL) return false;
if(i==word.length()) return node->ew;
if(word[i]=='.'){
for(int k=0;k<26;k++){
if(searchUtil(node->child[k], i+1, word)) return true;
}
} else {
int index=word[i]-'a';
if(searchUtil(node->child[index], i+1, word)) return true;
}
return false;
}
/** Initialize your data structure here. */
WordDictionary() {
root=createNode();
}
/** Adds a word into the data structure. */
void addWord(string word) {
insert(root, word);
}
/** 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 searchUtil(root, 0, word); // root, start index of word, word itself
}
};
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary* obj = new WordDictionary();
* obj->addWord(word);
* bool param_2 = obj->search(word);
*/