-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhone_directory_Tries.cpp
More file actions
66 lines (65 loc) · 1.88 KB
/
Copy pathPhone_directory_Tries.cpp
File metadata and controls
66 lines (65 loc) · 1.88 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<bits/stdc++.h>
using namespace std;
class Solution{
public:
vector<vector<string>> displayContacts(int n, string contact[], string s)
{
vector<vector<string>>ans;
struct TrieNode{
TrieNode* child[26];
bool fin;
TrieNode(){fin=false; for(int i=0;i<26;i++)child[i]=nullptr;}
};
TrieNode* rt=new TrieNode();
auto ins_trie = [&](string word){
TrieNode* curr=rt;
for(auto i:word)
{
if(curr->child[i-'a']==nullptr) curr->child[i-'a']=new TrieNode();
curr=curr->child[i-'a'];
}
curr->fin=true;
};
set<string>st;
//have to dfs for ans construction
function<void(TrieNode*,string)> dfs = [&](TrieNode* cr,string hld){
if(cr->fin) st.insert(hld);
for(int i=0;i<26;i++)
{
if(cr->child[i]!=nullptr)
{
hld.push_back('a'+i);
dfs(cr->child[i],hld);
hld.pop_back();
}
}
};
for(int i=0;i<n;i++) ins_trie(contact[i]);
TrieNode* tempo=rt;
string temp;
int lvl=0;
for(auto i:s)
{
lvl++;
vector<string>tempe;
temp.push_back(i);
if(tempo->child[i-'a']==nullptr) {
for(int it=lvl;it<=s.size();it++) ans.push_back({"0"});
break;
}
else
{
dfs(tempo->child[i-'a'],temp);
tempo=tempo->child[i-'a'];
}
if(st.empty()) ans.push_back({"0"});
else
{
for(auto i:st) tempe.push_back(i);
ans.push_back(tempe);
}
st.clear();
}
return ans;
}
};