-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_PhoneDirectory.cpp
71 lines (68 loc) · 1.78 KB
/
GFG_PhoneDirectory.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
/*
https://practice.geeksforgeeks.org/problems/phone-directory4628/1
Phone directory
*/
class Trie{
struct TrieNode{
TrieNode* children[26];
vector<int> indexes;
bool isEnd;
};
TrieNode *root;
public:
Trie() {
root = new TrieNode();
}
void insert(string& word, int index)
{
TrieNode* ptr = root;
int key=0;
for(char &c: word)
{
key = c-'a';
if(ptr->children[key] == nullptr)
ptr->children[key] = new TrieNode();
ptr = ptr->children[key];
ptr->indexes.push_back(index);
}
root->isEnd = true;
}
vector<vector<string>> searchContacts(string& word, string contact[])
{
vector<vector<string>> ans;
TrieNode *ptr = root;
int key=0;
int i=0;
for(i=0; i<word.length(); i++)
{
key = word[i]-'a';
if(ptr->children[key] == nullptr) break;
ptr = ptr->children[key];
vector<string> cur;
for(auto ids: ptr->indexes)
cur.push_back(contact[ids]);
ans.push_back(cur);
}
while(i++ < word.length())
ans.push_back({"0"});
return (ans);
}// end search
};
class Solution{
public:
vector<vector<string>> displayContacts(int n, string contact[], string s)
{
Trie tr;
sort(contact, contact+n); // for lexicographical increasing order
unordered_set<string> us; // to remove duplicates
for(int i=0; i<n; i++)
{
if(us.find(contact[i]) == us.end())
{
us.insert(contact[i]);
tr.insert(contact[i], i);
}
}
return tr.searchContacts(s, contact);
}
};