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

For example,

The idea is to construct a Trie from 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 insertion of a word.
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 the path from root to 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

Download   Run Code
Output:

A
BON
BOO
CAS
CAT
CH

Download   Run Code
Output:

A
BON
BOO
CAS
CAT
CH

Download   Run Code
Output:

A
BOO
BON
CAT
CAS
CH

source