电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母
class Solution(object):
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
if not digits:
return []
hash = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz"
}
def recur(path, i):
if i == n:
res.append("".join(path))
else:
nums = digits[i]
for num in hash[nums]:
path.append(num)
recur(path, i+1)
path.pop()
res, path, n = [], [], len(digits)
recur(path, 0)
return res