-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestUniquePrefix.cpp
More file actions
94 lines (90 loc) · 1.79 KB
/
ShortestUniquePrefix.cpp
File metadata and controls
94 lines (90 loc) · 1.79 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
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
/*
Question:
Shortest Unique Prefix
Asked in:
Google
Find shortest unique prefix to represent each word in the list.
Example:
Input: [zebra, dog, duck, dove]
Output: {z, dog, du, dov}
where we can see that
zebra = z
dog = dog
duck = du
dove = dov
NOTE : Assume that no word is prefix of another. In other words, the representation is always possible
*/
struct trieNode
{
int a[26];
struct trieNode* link[26];
};
struct trieNode* nn()
{
struct trieNode *n=new (struct trieNode);
memset(n->a,0,sizeof(n->a));
for(int i=0;i<26;i++)
n->link[i]=NULL;
return n;
}
void insert(struct trieNode* head,string s,int i)
{
if(i==s.size())
return ;
head->a[s[i]-'a']++;
if(head->a[s[i]-'a']==1){
head->link[s[i]-'a']=nn();}
insert(head->link[s[i]-'a'],s,i+1);
}
bool bar(struct trieNode *head)
{
for(int i=0;i<26;i++)
{
if(head->a[i])
return false;
}
return true;
}
void print(struct trieNode *head,string s)
{
if(bar(head)){cout<<s<<endl;
return ;}
for(int i=0;i<26;i++)
{
if(head->a[i])
{
s.push_back((char)(i+'a'));
print(head->link[i],s);
s.pop_back();
}
}
}
string foo(struct trieNode*head,string s,string ret,int i)
{
if(i==s.size())
return ret;
int count=0;
for(int i=0;i<26;i++)
{
count+=head->a[i];
}
if(count==1)
return ret;
ret.push_back(s[i]);
return foo(head->link[s[i]-'a'],s,ret,i+1);
}
vector<string> Solution::prefix(vector<string> &A) {
struct trieNode *head=nn();
for(int i=0;i<A.size();i++)
{
insert(head,A[i],0);
}
//print(head,"");
vector<string> ret;
for(int i=0;i<A.size();i++)
{
string z="";
ret.push_back(foo(head,A[i],z,0));
}
return ret;
}