Given a list of words in lexicographic order where no word is the prefix of another, find the shortest unique prefix to identify each word in the array uniquely.

For example,

Input:  [AND, BONFIRE, BOOL, CASE, CATCH, CHAR]
 
Output: [A, BON, BOO, CAS, CAT, CH]
 
Explanation:
 
A can uniquely identify AND
BON can uniquely identify BONFIRE
BOO can uniquely identify BOOL
CAS can uniquely identify CASE
CAT can uniquely identify CATCH
CH can uniquely identify CHAR

Practice this problem

The idea is to construct a Trie from the given words. While inserting the words in Trie, keep track of the total number of times each Trie node is visited. This can be easily done by maintaining an extra field in each Trie node for storing the frequency. Each Trie node’s frequency is initially 0 and incremented by one each time the Trie node is visited during the word’s insertion.

Finally, traverse the Trie in a preorder fashion to print the shortest unique prefix for each word in the Trie. For every Trie node being traversed, check its frequency. The prefix ending at a Trie node is the shortest unique prefix if its frequency is 1. Once the shortest unique prefix is identified, move to the next path in preorder traversal instead of traversing down further on the current Trie node.

Note that a prefix comprises all characters in a path from the root to a Trie node. The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

A
BON
BOO
CAS
CAT
CH

Java


Download  Run Code

Output:

A
BON
BOO
CAS
CAT
CH

Python


Download  Run Code

Output:

A
BOO
BON
CAT
CAS
CH

The time complexity of the above solution is O(N.M), where N is the total number of words and M is the maximum word length.