Given a dictionary of ancient origin where the words are arranged alphabetically, find the correct order of alphabets in the ancient language.

For example,

Input:  Ancient dictionary { ¥€±, €±€, €±‰ð, ðß, ±±ð, ±ßß }
 
Output: The correct order of alphabets in the ancient language is {¥ € ‰ ð ± ß}.
 
Since the input is small, more than one ordering is possible. Another such ordering is {¥ € ð ± ß ‰}.
 
 
Input:  Ancient dictionary { ÿ€±š, €€€ß, €€‰ð, ðß, ±ß¥š }
 
Output: The correct order of alphabets in the ancient language is {ÿ € ‰ ð ±}.
 
The alphabets {š, ß, ¥} are not included in the order as they are not properly defined.

If we carefully analyze the problem, we can see that it is a variation of Topological sorting of a DAG. A topological sorting of a directed acyclic graph is a linear ordering of its vertices such that for every directed edge (u —> v) from vertex u to vertex v, u comes before v in the ordering.

For example, consider the dictionary { ¥€±, €±€, €±‰ð, ðß, ±±ð, ±ßß } of ancient words. For each of the edges (x —> y) shown below, x should appear before y in the final ordering.

¥ ——> €
€ ——> ð, ‰
± ——> ß
ð ——> ±

If we perform topological sorting on the above graph, we get the correct order of alphabets in the ancient language: {¥ € ‰ ð ± ß} or {¥ € ð ± ß ‰}.

 
The idea is to iterate through the complete dictionary and compare adjacent words for a character mismatch. If a mismatch between adjacent words is seen, insert such a pair into a graph. The resultant graph is a DAG since all words in the dictionary are arranged alphabetically. Since the graph has no directed cycles, perform topological sorting on it, resulting in the correct order of alphabets in the ancient language.

The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

The correct order of alphabets in the ancient language is ¥ € ‰ ð ± ß

Java


Download  Run Code

Output:

The correct order of alphabets in the ancient language is ¥ € ‰ ð ± ß

Python


Download  Run Code

Output:

The correct order of alphabets in the ancient language is ¥ € ‰ ð ± ß

The time complexity of the above solution is O(N.M), where N is the dictionary size and M is the maximum length of a word in the dictionary.