Consider a new order of english alphabets (a to z), that we are not aware of. What we have though, dictionary of words, ordered according to the new order.
Our task is to give each alphabet a rank, ordered list of words. The rank of an alphabet is the minimum integer R that can be given to it, such that:
if alphabet Ai has rank Ri and alphabet Aj has Rj, then
Points to note:
Input:
Output:
The alphabets, in order of their Rank, for alphabets with the same rank, print them in the order as we know it today
Examples
1)
Input: 2 ba ab
Output: b a
Explanation: ab comes after ba implies that b < a
2)
Input: 3 abc aca ca
Ouput: ab c
Explanation: from the first 2 words, we know b < c, and from the last 2 words, we know a < c. We don't have an ordering between a and b, so we give both of them the same rank, and print them in the order as we know today: ab