17 电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
示例:
输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
解法
- Python
class Solution:
def letterCombinations(self, string: str) -> List[str]:
nine = [' ','','abc','def','ghi','jkl','mno','pqrs','tuv','wxyz']
length = len(string)
if length == 0: return []
res = [None for i in range(length)]
total = []
def find(string,index):
if length-index==1:
for i in nine[eval(string)]:
res[length-1] = i
total.append(''.join(res.copy()))
return
for i in nine[eval(string[0])]:
res[index]=i
find(string[1:],index+1)
find(string,0)
return total