Alien Symbol Order Problem

Alien Symbol Order Problem — ExecCode Hard DSA Practice

Solve the Alien Symbol Order problem on ExecCode. Free online hard DSA practice in Graph. Write and run code in Java, C++, Python — no signup required to run.

Problem description

An alien dictionary is a sorted list of words under an unknown alphabet order. Adjacent words give inequality edges between the first mismatched letters. You must recover one valid total order. Invalid input (cycle, prefix contradiction) should return an empty string. When several orders are valid, return the lexicographically smallest. Return the smallest lexicographic valid alphabet string, or "" if the words contradict a total order. You are given: words — non-empty list of lowercase words, already sorted in alien order Return a string of unique letters appearing in words, or empty on contradiction. Use only letters that appear in the input.

Examples

Input {'words': ['wrt', 'wrf', 'er', 'ett', 'rftt']}; Output wertf. Input {'words': ['z', 'x']}; Output zx. Input {'words': ['z', 'x', 'z']}; Output

Constraints

If a longer word is listed before its own prefix, that is invalid Unused letters that never appear are omitted

Practice Alien Symbol Order free on ExecCode. Browse DSA problems, topic map, and placement guides.