Shortest Unique Prefix

Find shortest unique prefix to represent each word in the list.


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.

Interview Code Editor
  • Hint 1
  • Solution Approach
  • Complete Solution
3944 successful submissions.
Asked In:
  • Google
Click here to jump start your coding interview preparation