Ask a Question

 

Start Coding Today

   

GeeksQuiz

GeeksforGeeksIDE

Data Structures

Algorithms

Interview Experiences

C Programming

C++ Programming

Java Programming

GATE CS

Books

Find shortest unique prefix to represent each word in the list. [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.

asked Oct 10, 2015 by Swanky

3 Answers

+1 vote
we will build prefix tree (trie) and we will also 
store count of characters

                root
                /|
         (d, 3)/ |(z, 1)
              /  |
          Node1  Node2
           /|        \
     (o,2)/ |(u,1)    \(e,1)
         /  |          \
   Node1.1  Node1.2     Node2.1
      | \         \            \
(g,1) |  \ (t,1)   \(c,1)       \(b,1)
      |   \         \            \ 
    Leaf Leaf       Node1.2.1     Node2.1.1
    (dog)  (dot)        \                  \
                         \(k, 1)            \(r, 1)
                          \                  \   
                          Leaf               Node2.1.1.1
                          (duck)                       \
                                                        \(a,1)
                                                         \
                                                         Leaf
                                                         (zebra)

Now, for every leaf / word , we find the character 
nearest to the root with frequency as 1. 
The prefix that the path from root to this character 
corresponds to, is the representation of the word. 
#include<bits/stdc++.h>
using namespace std;


int IDX;
vector<vector<int> > Cnt;
vector<vector<int> > Trie;
     
void insert(string str)
{
    int len = str.size();
    int root = 0;
    int i = 0;
 
    while(i < len) {
        int chr = str[i] - 'a';
        if(Trie[root][chr] == -1) {
            ++IDX;
            Trie[root][chr] = IDX;
            Cnt[root][chr] = 1;
        } 
        else {
            Cnt[root][chr]++;
        }
        root = Trie[root][chr];
        i++;
    }
}
 
string Query(string str) {
     
    int root = 0;
    string ans = "";
    int i = 0;
    int len = str.size();
 
    while(i < len) {
 
        int chr = str[i] - 'a';
        ans += str[i];
        if(Cnt[root][chr] == 1) return ans;
        root = Trie[root][chr];
        i++;
    }
 
    return "";
}
 
vector<string> prefix(vector<string> Vec) {
         
    int mx_len = 0;
    int N = Vec.size();
    for(int i = 0; i < N; ++i) {
        mx_len = max(mx_len, (int)Vec[i].size());
    }
 
    Trie.clear();
    Cnt.clear();
    Trie.resize(N * mx_len + 1, vector<int>(27, -1));
    Cnt.resize(N * mx_len + 1, vector<int>(27, -1));
    IDX = 0;
 
    for(int i = 0; i < N; ++i) {
        insert(Vec[i]);
    } 
 
    vector<string> Ans;
    for(int i = 0; i < N; ++i) {
        string ans = Query(Vec[i]);
        Ans.push_back(ans); 
    }
 
    return Ans;
}

int main() {
	
	string str[] = {"zebra", "dog", "duck", "dove"};
	vector<string> Vec(str, str + sizeof str / sizeof str[0]);
	vector<string> ans = prefix(Vec);
	
	for(int i = 0; i < ans.size(); i++) cout << ans[i] << " ";
	
	return 0;
}

 

answered Oct 10, 2015 by Gaurav
0 votes

can u explain it more ?

i am not able to understand the solution.

answered Jun 8, 2016 by anonymous
0 votes

can u provide java code for this?

answered May 8 by Raghawendra singh 1

Please log in or register to answer this question.

...