FastPrepClassify an Alien Dictionary Ordering

Classify an Alien Dictionary Ordering

ZipRecruiter logoZipRecruiter● HardNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

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 AMBIGUOUS when multiple orders are valid.
  • Return INVALID for a cycle or when a longer word precedes its exact prefix.

Function

alienDictionaryOrder(words: String[]) → String

Examples

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.

More ZipRecruiter problems

See ZipRecruiter hiring insights
public String alienDictionaryOrder(String[] words) {
    // Write your code here.
}
words["wdc","wdr","cd","dd","dr"]
expected"wcdr"
Checking account…