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

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。