LeetCode 269 Alien Dictionary - Solution

The core idea

Compare adjacent words to extract ordering rules. If word[i] and word[i+1] differ at position j, then word[i][j] < word[i+1][j].

Build a directed graph where an edge from 'a' to 'b' means 'a' < 'b'.

Topological sort gives a valid character ordering. If a cycle exists, no valid ordering is possible.

Edge case: if a longer word appears before its prefix (like "abc" before "ab"), the input is invalid.