hardAdvanced GraphsFacebookGoogleAmazon
Lexicon Ordering
Linguists infer a lost alphabet's letter order by comparing adjacent words sorted in that unknown alphabet.
Given a list of words sorted lexicographically in an alien alphabet, return a string containing the alphabet's letters in order. Return '' if impossible.
Choose approach
e
f
r
t
w
cur
1/6
Narrationstep 1 of 6
Peel 'w' (zero in-degree). Order so far: "w".
Variables
c=w
order=w
How to think about it
- Only the FIRST differing character per adjacent pair is an informative constraint.
- A prefix inconsistency (longer word before its prefix) is invalid.
- Topological sort yields any valid order; cycle -> ''.
Edge cases to catch in interview
- Single word -> any permutation of its chars.
- Prefix violation -> ''.
Examples
Example 1
"wertf"{"words":["wrt","wrf","er","ett","rftt"]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed