150强访谈简单

外星人词典

“外星人字典”问题的详细指南和 Python 实现。

问题陈述

简单

有一种新的外星语言使用英语字母。但是,您不知道字母的顺序。

您将获得外来语言词典中的字符串单词列表,其中单词中的字符串根据这种新语言的规则按字典顺序排序。

返回新外来语言中唯一字母的字符串,按照新语言的规则按字典顺序递增排序。如果无解,则返回“”。如果有多个解决方案,则返回其中任何一个。

编写一个函数 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?
考虑使用高级图特定的数据结构,例如集合或堆。
Edge Cases to Watch
  • 空输入结构
  • 单元素输入
  • 大数值范围

准备好解决了吗?

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 资源

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。