Skip to content

Commit

Permalink
Add trie implementation to find shortest unique prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
nandwalritik committed Jun 7, 2021
1 parent 8abad2b commit 083f0eb
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions Trie/shortestUniqPref.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <bits/stdc++.h>
using namespace std;
/*
time complexity for Trie construction -> O(n) -> n is total characters in words
finding character nearest to root with freq 1 -> O(n) -> n is total characters in words
*/
struct Trie
{
Trie *child[26] = {NULL};
bool isLeaf = false;
int cnt = 1;
};

void insert(Trie *root, string s)
{
int n = s.size();
for (int i = 0; i < n; i++)
{
int key = s[i] - 'a';
if (root->child[key] == NULL)
{
root->child[key] = new Trie;
}else{
(root->child[key]->cnt)++;
}
root = root->child[key];
}
root->isLeaf = true;
}
void findPref(Trie *root, string s, vector<string> &ans)
{
if (root && root->cnt == 1)
{
ans.push_back(s);
return;
}
if (root == NULL)
return;
for (int i = 0; i < 26; i++)
{
if (root->child[i])
{
findPref(root->child[i], s + (char)(i + 'a'), ans);
}
}
}
int main()
{
vector<string> v{"geeksgeeks", "geeksquiz", "geeksforgeeks"};
vector<string> ans;
int n = v.size();
Trie *root = new Trie;
for (int i = 0; i < n; i++)
{
insert(root, v[i]);
}
for (int i = 0; i < 26; i++)
{
string s = "";
if (root->child[i])
{
findPref(root->child[i], s + (char)(i + 'a'), ans);
}
}
for (auto x : ans)
{
cout << x << " ";
}
cout << endl;
return 0;
}

0 comments on commit 083f0eb

Please sign in to comment.