Alien Dictionary Order II

Given a sorted list of words from an alien language, return a valid character ordering. Return an empty string if no valid ordering exists. Pattern focus: DFS Topological Order. This is the postorder/finish-time variant of the alien dictionary problem.

Input Format

words = dictionary words sorted according to an alien alphabet

Output Format

valid character order or an empty string

Constraints

  • 1 <= words.length <= 200
  • 1 <= words[i].length <= 100
  • words contain only lowercase English letters

Examples

Example 1:

Input:

words = ["wrt","wrf","er","ett","rftt"]

Output:

wertf

Explanation:

The adjacent words imply the order w -> e -> r -> t -> f.

Example 2:

Input:

words = ["z","x"]

Output:

zx

Explanation:

The first word before the second gives z < x.

Example 3:

Input:

words = ["z","x","z"]

Output:

Explanation:

The constraints create a cycle, so there is no valid order.

Loading...
Alien Dictionary Order II - Topological Sort