An alien spaceship arrived at our planet Earth. An alien dropped his dictionary of words on the way back to his planet. Ninja found that dictionary and now wants to create the order of the characters of that language. Help ninja in creating the order of the characters with the help of the list of words given as were found in the Alien’s dictionary.
Example:Let us assume that we have a list of words found in the dictionary in the same order as given: [“baa”, “abcd”, “abca”, “cab”, “cad”]
Now, ninja needs to find the order of the characters used in these strings.
The order would be: ‘b’, ‘d’, ‘a’, ‘c’, because “baa” comes before “abcd”, hence ‘b’ will come before ‘a’ in the order, similarly because, “abcd” comes before “abca”, ‘d’ will come before ‘a’. And so on.
Note:
A certain list of words might have more than one correct order of characters. In such cases, you need to find the smallest in normal lexicographical order. In the case of INVALID ORDER, simply return an empty string.
Invalid Order:
words = [“aba”, “bba”, “aaa”].
In this case, no valid order is possible because, from the first two words, we can deduce ‘a’ should appear before ‘b’, but from the last two words, we can deduce ‘b’ should appear before ‘a’ which is not possible.
More than one correct order:
words = [“ca”, “cb”].
In this case, we only know that ‘b’ will come after ‘a’ in the order of characters, but we don't have any knowledge of ‘c’. So, the valid correct orders can be = “abc”, “cab”, “acb”. Out of these, “abc” is lexicographically smallest, hence it will be printed.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain an integer ‘N’, which denotes the number of words in the dictionary.
The second line of each test case will contain ‘N’ space-separated strings which denote the words of the dictionary.
Output Format:
For each test case, print the order of characters.
Output for every test case will be printed in a separate line.
1 <= T <= 10
1 <= N <= 300
0 <= size <= 100
Time limit: 1 sec
2
5
wrt wrf er ett rftt
2
z x
wertf
zx
In the first test case,
from "wrt"and"wrf" ,we can get 't' < 'f'
from "wrt"and"er" ,we can get 'w' < 'e'
from "er"and"ett" ,we can get 'r' < 't'
from "ett"and"rftt" ,we can get 'e' < 'r'
So the order of characters will be "wertf”
In the second test case,
from "z" and "x",we can get 'z' < 'x'
So the order of characters will be "zx"
3
5
baa abcd abca cab cad
3
caa aaa aab
2
ca cb
bdac
cab
abc
Can you think about creating a graph
The idea behind this approach is to create a graph of the words of the dictionary and then apply the topological ordering of the graph.
A topological ordering of a directed 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.
The approach is to take two words from the array of words and then compare characters of both words and find the first character that is not the same in the words.
Then, create an edge in the graph from the first different character of the first word to the second word.
Finally, apply topological sorting in the above-created graph.
Let us understand this better by dividing the complete solution into two parts:
Algorithm + Pseudo Code:
Keep in mind the following cases:
O(N + W), where ‘N’ is the number of words in the array, and ‘W’ is the size of words.
The time complexity of topological sorting is O(V + E), where ‘V’ is the number of vertices and ‘E’ is the number of edges, because in the worst case the algorithm will traverse through the complete graph, that is V vertices and E - 1 edges. So, the time complexity is O(N + W).
O(N), where ‘N’ is the number of words in the array
Extra space required for data structures used like queue and maps. So, space complexity is O(N).