forked from Masked-coder11/gfg-POTD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
31.01.2024.cpp
54 lines (47 loc) · 1.36 KB
/
31.01.2024.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
// User function template for C++
// trie node
/*
struct TrieNode {
struct TrieNode *children[ALPHABET_SIZE];
// isLeaf is true if the node represents
// end of a word
bool isLeaf;
};
*/
class Solution
{
public:
//Function to insert string into TRIE.
void insert(struct TrieNode *root, string key)
{
// code here
int n=key.length();
TrieNode* temp=root;
for(int i=0;i<n;i++){
int ind= CHAR_TO_INDEX(key[i]);
if(temp->children[ind]==NULL){
temp->children[ind]=getNode();
}
temp=temp->children[ind];
}
temp->isLeaf=true;
}
//Function to use TRIE data structure and search the given string.
bool search(struct TrieNode *root, string key)
{
// code here
int n=key.length();
TrieNode* temp=root;
for(int i=0;i<n;i++){
int ind= CHAR_TO_INDEX(key[i]);
if(temp->children[ind]==NULL){
return false;
}
temp=temp->children[ind];
}
if(temp!=NULL && temp->isLeaf==true){
return true;
}
return false;
}
};