Airport Codes
InterviewTime limit8sMemory limit512 MB
For each set of airport names, find the smallest k so that the derived codes (first letter plus letters after vowels, truncated to k) are all distinct, or report -1.
- Level
Medium5 of 10
- Topics
- String, Hash map, Sorting, Binary search
- Solved
- No attempts yet
Problem
In the kingdom of JAG, each domestic airport is identified by an airport code.
An airport code is assigned from the airport's name written in lowercase English letters by the following rules:
- Take the first character of the name and each character that immediately follows a vowel (a, i, u, e, o), in order.
- If the resulting string is shorter than
kcharacters, use it as the airport code. If it is at leastkcharacters, use the firstkcharacters of the resulting string as the airport code.
For example, when k = 3, haneda gets the code hnd, oookayama gets ooo, and tsu gets t.
With this assignment method, however, different airport names can end up with the same code, which causes confusion. Given a list of airport names, write a program that decides whether all airports can receive distinct codes. If so, find the smallest k that makes all airport codes distinct. If not, report that fact.
Input
The input consists of at most 100 datasets. Each dataset has the following format.
n
s1
...
sn
The first line gives the number of airports n (2 ≤ n ≤ 50) as an integer, and the following n lines each give an airport name si as a string. An airport name consists only of lowercase English letters from 'a' to 'z', and every name has length between 1 and 50 inclusive. All given airport names are distinct. That is, for 1 ≤ i < j ≤ n, si ≠ sj.
The end of the input is indicated by a line containing a single zero.
Output
For each dataset, if distinct airport codes can be assigned to all airports, print the smallest such k on one line. If it is impossible, print -1 on one line.