Top 150 Interview簡単

Alien Dictionary

Detailed guide and Python implementation for the 'Alien Dictionary' problem.

問題提起

簡単

There is a new alien language that uses the English alphabet. However, the order of the letters is unknown to you.

You are given a list of strings words from the alien language's dictionary, where the strings in words are sorted lexicographically according to the rules of this new language.

Return a string of the unique letters in the new alien language sorted in lexicographically increasing order by the new language's rules. If there is no solution, return "". If there are multiple solutions, return any of them.

Write a function alienOrder(words: List[str]) -> str.

制約
  • 1 <= len(words) <= 100
  • 1 <= len(words[i]) <= 100
  • words[i] consists of lowercase English letters

Example 1
Input
words = ["wrt","wrf","er","ett","rftt"]
Output
"wertf"
Explanation

From the sorted words, we can derive the relationships: 'w' -> 'e', 'r' -> 't', 't' -> 'f', etc.

Example 2
Input
words = ["z","x"]
Output
"zx"
Explanation

From "z" and "x", we know 'z' comes before 'x'.

Example 3
Input
words = ["z","x","z"]
Output
""
Explanation

The order is invalid because 'z' comes before 'x' and 'x' comes before 'z'.

Need a Hint?
Consider using Advanced Graphs-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

解決する準備はできましたか?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

エディタで開く
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。