Classify an Alien Dictionary Ordering
Problem statement
words is intended to be sorted under an unknown alphabet. Derive precedence edges from the first differing character of every adjacent pair and include every observed character.
- Return the unique character order when exactly one topological ordering exists.
- Return
AMBIGUOUSwhen multiple orders are valid. - Return
INVALIDfor a cycle or when a longer word precedes its exact prefix.
Function
alienDictionaryOrder(words: String[]) → StringExamples
Example 1
words = ["wdc","wdr","cd","dd","dr"]return = "wcdr"The adjacent differences force the unique order wcdr.
Example 2
words = ["ab","ac"]return = "AMBIGUOUS"Only b before c is fixed, leaving a unconstrained.
Constraints
0 <= words.length <= 10000- The total number of lowercase letters is at most
100000.