17. Letter Combinations of a Phone Number
給定一數字字串,每一數字都有其可能對應到的字母,求出此數字字串可能對應到的所有字母字串組合
如果是空字串,字母字元個別放入一空陣列res,結束後回傳res,若不為空則依序加入字典所代表的值
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
if digits == '':
return []
digit_chr = {'2':'abc', '3':'def', '4':'ghi', '5':'jkl', '6':'mno', '7':'pqrs', '8':'tuv', '9':'wxyz'}
output =list(digit_chr[digits[0]])
for digit in digits[1:]:
temp= []
for str1 in output:
for str2 in list(digit_chr[digit]):
temp.append(str1+str2)
output = temp
return output