Infer a valid alien alphabet from words already sorted in that language. Build precedence constraints from adjacent words, reject invalid prefix orderings and cycles, and produce the lexicographically smallest topological order when several answers are possible.
## Problem
You are given a list of words sorted according to an unknown alphabet.
Return one valid ordering of every distinct character that appears in the
words. If the ordering is invalid or impossible, return an empty string.
When more than one ordering is valid, return the lexicographically smallest
valid ordering under ordinary character order so the output is deterministic.
### Constraints & Assumptions
- The list contains between 1 and 1,000 lowercase words.
- The total number of characters across all words is at most 100,000.
- Every distinct character must appear exactly once in a non-empty result.
- A longer word appearing before its exact prefix makes the input invalid.
### Clarifications
- Only the first differing character in each adjacent word pair creates an ordering constraint.
- Duplicate constraints must not increase a character's in-degree more than once.
- A cycle means no alphabet can satisfy the supplied ordering.
### Examples
```text
words = ["wrt", "wrf", "er", "ett", "rftt"]
output = "wertf"
words = ["abc", "ab"]
output = ""
```
### Hints
```hint Compare neighboring words
Find the first position where each adjacent pair differs; later positions do not add constraints.
```
```hint Make ties deterministic
Consider which topological-sort frontier structure always selects the smallest available character.
```
Quick Answer: Infer a valid alien alphabet from words already sorted in that language. Build precedence constraints from adjacent words, reject invalid prefix orderings and cycles, and produce the lexicographically smallest topological order when several answers are possible.
You are given a list of words sorted according to an unknown alphabet.
Return one valid ordering of every distinct character that appears in the
words. If the ordering is invalid or impossible, return an empty string.
When more than one ordering is valid, return the lexicographically smallest
valid ordering under ordinary character order so the output is deterministic.
Constraints & Assumptions
The list contains between 1 and 1,000 lowercase words.
The total number of characters across all words is at most 100,000.
Every distinct character must appear exactly once in a non-empty result.
A longer word appearing before its exact prefix makes the input invalid.
Clarifications
Only the first differing character in each adjacent word pair creates an ordering constraint.
Duplicate constraints must not increase a character's in-degree more than once.
A cycle means no alphabet can satisfy the supplied ordering.
Examples
words = ["wrt", "wrf", "er", "ett", "rftt"]
output = "wertf"
words = ["abc", "ab"]
output = ""