-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathdesign-add-and-search-words-data-structure.cpp
77 lines (77 loc) · 1.7 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
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Trie
{
public:
vector<Trie*> x;
Trie():x(26){}
string word;
};
Trie *root;
void addWord(string word)
{
Trie *temp=root;
for(int i=0;i<word.size();i++)
{
if(temp->x[word[i]-'a']==NULL)
temp->x[word[i]-'a']= new Trie;
temp=temp->x[word[i]-'a'];
if(i+1==word.size())
temp->word=word;
}
}
void searchtrie(string word,Trie* temp,bool &v,int start)
{
if(temp->word.size()==word.size())
v=true;
else
{
for(int i=start;i<word.size()&&v==false;i++)
{
if(word[i]=='.')
{
for(int j=0;j<26&&v==false;j++)
if(temp->x[j]!=NULL)
searchtrie(word,temp->x[j],v,i+1);
}
else
{
if(temp->x[word[i]-'a']!=NULL)
{
temp=temp->x[word[i]-'a'];
if(temp->word.size()==word.size())
v=true;
}
else
break;
}
}
}
}
bool search(string word)
{
bool v=false;
Trie *temp=root;
searchtrie(word,temp,v,0);
return v;
}
int main()
{
root=new Trie;
int present;
vector<string> words={"Hello","World","C++Program"}; //Sample Input
vector<string> find={"World","Program"};
for(int i=0;i<words.size();i++)
addWord(words[i]);
for(int i=0;i<find.size();i++)
{
present=search(find[i]);
if(!present)
printf("Word not Found :(\n");
else
printf("Found the Word :)\n");
}
return 0;
}