| Topic | Difficulty | Companies |
|---|---|---|
| Trie | HARD | Google |
You are given a list of strings. You need to find the shortest unique prefix for each string.
Problem Note:
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.