You are given n distinct contact names with the guarantee that no name is a prefix of another. For every name, output its shortest unique prefix: the shortest prefix such that no other name in the list has that same prefix.
Because a prefix of a given length is a single fixed string, the shortest unique prefix of each name is unique. Names consist of lowercase English letters only.
Line 1: an integer n.
Line 2: n space-separated distinct names (no name is a prefix of another).
Print n lines; line i is the shortest unique prefix of the i-th name, in input order.
Example 1
Input
3 dog cat cow
Expected
d ca co
Explanation
dog is the only name starting with d, so d. cat and cow share c, so ca and co distinguish them.
Example 2
Input
2 a b
Expected
a b
Explanation
Each single-letter name is already unique, so the prefixes are a and b.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →