You are given a dictionary of words sorted according to the rules of an unknown alien language.
Using the sorted dictionary, determine a valid ordering of the alien alphabet.
If multiple valid orderings exist, return the lexicographically smallest valid ordering.
If the dictionary is invalid (contains a contradiction), print:
INVALID
A contradiction occurs when:
Print:
INVALID if no valid ordering exists.1 ≤ N ≤ 10^510^55
baa abcd abca cab cad
bdac
From the given dictionary:
baa → abcd gives b < aabcd → abca gives d < aabca → cab gives a < ccab → cad gives b < dThese constraints produce the ordering:
b < d < a < c
Hence, the lexicographically smallest valid ordering is:
bdac
If a cycle exists or a word appears before its own prefix (e.g., abcd before ab), the output should be:
INVALID
Intuit • Pending
Intuit • Pending
Intuit • Pending
Flipkart • Pending