You are given a list of strings. You need to find the shortest unique prefix for each string.

Problem Note:

  • The shortest unique prefix of a string is the prefix of that string such that no other string in the list has that prefix.
  • Assume that no word is a prefix of another. This ensures that there is always a valid representation.
  • All prefixes need not be of the same length
  • The expected time complexity is O( nlogn ) or better.
Example 1
Input: {"alphabet", "carpet", "cartoon", "carrot", "alpine"}
Output: {"alph", "carp", "cart", "carr", "alpi"}
Explanation: "alphabet" and "alpine" have common prefix "alp". So, shortest unique prefix to differ them is "alph" and "alpi". 
Similarly, for other words "car" is the common prefix. So, the answer is "carp", "cart", "carr".
Example 2
Input: {"create", "big"}
Output: {"c", "b"}
Explanation: All words in the list have no common prefix. So, only first letter of the words,"c" and "b", can represent them uniquely.