Letter Combinations of a Phone Number
LeetCode 17
class Solution:
def letterCombinations(self, nums):
"""
:type nums: str
:rtype: List[str]
"""
self.dict = {
'1': '*',
'2': 'abc',
'3': 'def',
'4': 'ghi',
'5': 'jkl',
'6': 'mno',
'7': 'qprs',
'8': 'tuv',
'9': 'wxyz',
'0': ' '
}
if not nums: return []
self.res = []
self.dfs(nums, "", 0)
return self.res
def dfs(self, nums, path, index):
if len(path) == len(nums):
self.res.append(path)
return
else:
for c in self.dict[nums[index]]:
self.dfs(nums, path+c, index+1)
Last updated