-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path78_designaddandsearchwordsdatastructure.cpp
More file actions
52 lines (47 loc) · 1.82 KB
/
78_designaddandsearchwordsdatastructure.cpp
File metadata and controls
52 lines (47 loc) · 1.82 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
//https://leetcode.com/problems/design-add-and-search-words-data-structure/description/
struct TrieNode {
bool end; // Flag to indicate whether this node represents the end of a word or not
vector<TrieNode*>children=vector<TrieNode*>(26,nullptr); // Vector of pointers to child nodes for each possible character in the alphabet (26 lowercase English letters)
};
class WordDictionary {
public:
TrieNode* root;
WordDictionary() {
root=new TrieNode();
}
// Insert a word into the Trie
void addWord(string word) {
TrieNode* curr=root;
for(char c:word){
int index=c-'a';
if(!curr->children[index]){
curr->children[index]=new TrieNode();
}
curr=curr->children[index];
}
curr->end=true; // Mark the last node as the end of the word
}
// Search for a word in the Trie
bool pathSearch(string word, TrieNode* root, int index){
if(index==word.size()) return root->end; // return true if the last node is marked as the end of a word
TrieNode* node=root;
char c=word[index];
int idx=c-'a';
if(c=='.'){ // if character is '.' check for remaining characters in all nodes
for(int i=0;i<26;i++){
if(node->children[i]){
bool search= pathSearch(word,node->children[i],index+1);
if(search) return true;
}
}
return false;
}
else{
if(!node->children[idx])return false; // if the current character doesn't exist return false
return pathSearch(word,node->children[idx],index+1); // if current character exists check for remaining characters
}
}
bool search(string word) {
return pathSearch(word,root,0);
}
};