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

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