Took me a minute to see past the alien language flavor text and realize this is just topological sort.
Model the problem as a directed graph where each letter is a node and edges represent the relative order derived from adjacent words. Perform a topological sort to find a valid ordering, detecting cycles to return an empty string if none exists. Handle edge cases like invalid prefixes and duplicate words.
Pro tip: When comparing adjacent words, only the first differing character gives a valid ordering constraint; also, if a shorter word appears after a longer word with the shorter as a prefix, the ordering is invalid. Explicitly check these cases to avoid incorrect results.
Initialize a set of all unique characters and an adjacency list. For each pair of adjacent words, find the first differing character and add a directed edge from the character in the first word to the character in the second word.
While comparing adjacent words, if the first word is longer and the second word is a prefix of the first, return an empty string immediately because the ordering is invalid.
Perform a topological sort on the graph using either Kahn's algorithm (BFS with in-degrees) or DFS with cycle detection. If a cycle is detected, return an empty string.
If the topological sort completes without cycles, concatenate the nodes in the order they are visited to form the alien alphabet string. Return this string.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.