-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathShortestUniquePrefix.cpp
77 lines (67 loc) · 2 KB
/
ShortestUniquePrefix.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
/*
Shortest Unique Prefix
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
*/
// Create trie of string, for each node keep track of freq the number of time node has been visited for finding prefix check if the count is 1 and return
struct Trie
{ // Struct to create trie
int count;
Trie *child[26];
};
struct Trie *trieNode(void)
{ // Function to initialize trie node with child and assign null to them and return the node
struct Trie *new_trie = new Trie;
new_trie->count = 1;
for (int i = 0; i < 26; i++)
{
new_trie->child[i] = NULL;
}
return new_trie;
}
void insert(Trie *root, string s)
{ // Insert strings into Trie
struct Trie *step = root;
for (int i = 0; i < s.length(); i++)
{
int idx = s[i] - 'a';
if (!step->child[idx]) // if node doesnt exist create new node with its child
step->child[idx] = trieNode();
else
(step->child[idx]->count)++; // else inc the count of the child node
step = step->child[idx]; // move to child node
}
}
string findPrefix(Trie *root, string s)
{ //Function to find prefix
Trie *temp = root;
string ans = "";
for (int i = 0; i < s.length(); i++)
{ //check till string length
int c = s[i] - 'a';
root = root->child[c]; // move to character node
ans += s[i]; // add to result
if (root->count == 1) // if current node freq is one return
return ans;
}
}
vector<string> Solution::prefix(vector<string> &A)
{
struct Trie *root = trieNode(); // Call function to create root node, with all its child initialized to NULL
for (auto a : A)
{
insert(root, a);
}
vector<string> res;
for (auto a : A)
{
res.emplace_back(findPrefix(root, a)); //Calling fndPrefix to get result and store in vector for each string
}
return res;
}