-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Auto_complete.cpp
172 lines (139 loc) · 3.42 KB
/
Auto_complete.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* Given n number of words and an incomplete word w. You need to auto-complete that word w.
That means, find and print all the possible words which can be formed using the incomplete word w.
Note : Order of words does not matter. */
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class TrieNode {
public:
char data;
TrieNode ** children;
bool isTerminal;
TrieNode(char data) {
this -> data = data;
children = new TrieNode * [26];
for (int i = 0; i < 26; i++) {
children[i] = NULL;
}
isTerminal = false;
}
};
class Trie {
TrieNode * root;
public:
int count;
Trie() {
this -> count = 0;
root = new TrieNode('\0');
}
bool insertWord(TrieNode * root, string word) {
// Base case
if (word.size() == 0) {
if (!root -> isTerminal) {
root -> isTerminal = true;
return true;
} else {
return false;
}
}
// Small Calculation
int index = word[0] - 'a';
TrieNode * child;
if (root -> children[index] != NULL) {
child = root -> children[index];
} else {
child = new TrieNode(word[0]);
root -> children[index] = child;
}
// Recursive call
return insertWord(child, word.substr(1));
}
// For user
void insertWord(string word) {
if (insertWord(root, word)) {
this -> count++;
}
}
TrieNode * complete(TrieNode * root, string s, string pattern) {
if (s.size() == 0) {
if (root -> isTerminal == true) {
cout << pattern << endl;
}
return root;
}
TrieNode * child;
int index = s[0] - 'a';
if (root -> children[index] != NULL) {
child = root -> children[index];
} else {
return NULL;
}
TrieNode * ans = complete(child, s.substr(1), pattern);
return ans;
}
void print(TrieNode * root, string s) {
for (int i = 0; i < 26; i++) {
TrieNode * child = root -> children[i];
if (child != NULL) {
if (child -> isTerminal == true) {
string temp = s;
cout << temp << child -> data << endl;
child -> isTerminal = false;
temp = temp + child -> data;
print(child, temp);
} else {
string temp = s;
temp = temp + child -> data;
print(child, temp);
}
}
}
}
void autoComplete(vector < string > input, string pattern) {
// Print the output as specified in question
for (int i = 0; i < input.size(); i++) {
insertWord(input[i]);
}
TrieNode * ans = complete(root, pattern, pattern);
if (ans != NULL) {
print(ans, pattern);
}
}
};
int main() {
Trie t;
int n;
cout << "enter number of complete words to be stored in trie" << endl;
cin >> n;
vector < string > vect;
cout << " enter the words " << endl;
for (int i = 0; i < n; ++i) {
string word;
cin >> word;
vect.push_back(word);
}
string pattern;
cout << " enter the incomplete word" << endl;
cin >> pattern;
cout << "the words that starting from "<< pattern<< " are"<<endl;
t.autoComplete(vect, pattern);
}
/*
TIme Complexity - O(N*M)
Space Complexity - O(N*M)
where N is the number of words in trie and M is the average word length.
Sample Input 1-
enter number of complete words to be stored in trie
7
enter the words
do dont no not note notes den
enter the incomplete word
no
Sample output 1 -
the words that starting from no are
no
not
note
notes
*/