150強訪談簡單

電話號碼的字母組合

“電話號碼的字母組合”問題的詳細指南和 Python 實現。

問題陳述

簡單

給定一個包含 2 到 9 之間數字的字串,傳回該數字可以表示的所有可能的字母組合。以任意順序回傳答案。

下面給出了數字到字母的映射(就像電話按鈕一樣)。請注意,1 不映射到任何字母。

2: abc、3: def、4: ghi、5: jkl、6: mno、7: pqrs、8: tuv、9: wxyz

實作函數 letterCombinations(digits: str) -> list

約束條件
  • 0 <= digits.length <= 4
  • digits[i] is a digit in the range ['2', '9']

範例

Example 1
Input
"23"
Output
["ad","ae","af","bd","be","bf","cd","ce","cf"]
Explanation

Digit 2 maps to 'abc' and digit 3 maps to 'def'. All combinations of one letter from each digit are generated.

Example 2
Input
""
Output
[]
Explanation

Empty input produces no combinations.

Example 3
Input
"2"
Output
["a","b","c"]
Explanation

Digit 2 maps to 'abc'.

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

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