Word Sorting
InterviewTime limit2sMemory limit256 MB
Deduplicate a list of words and sort them first by length, then lexicographically.
Problem
You are given N lowercase English words. Remove duplicate words, then sort the remaining words by the following rules.
- A shorter word comes first.
- If two words have the same length, the lexicographically smaller word comes first.
Input
The first line contains the number of words N. (1 ≤ N ≤ 20,000)
Each of the next N lines contains one lowercase English word. The length of each word is at most 50.
Output
Print the unique words in the required order, one word per line.