Alien Dictionary Problem

Alien Dictionary Problem — ExecCode Hard DSA Practice

Solve the Alien Dictionary 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

There is a new alien language that uses the English alphabet. However, the order among letters is unknown. You are given a list of words from the alien language's dictionary, sorted lexicographically by the rules of this new language. Derive the order of letters in this language and return it as a string. If the order is invalid, return an empty string. If multiple valid orders exist, return any.

Examples

Input words = ["wrt", "wrf", "er", "ett", "rftt"]; Output "wertf". Input words = ["z", "x"]; Output "zx". Input words = ["z", "x", "z"]; Output ""

Constraints

1 ≤ words.length ≤ 100 1 ≤ words[i].length ≤ 100 words[i] consists of lowercase English letters.

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