-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathletterCombinations.py
More file actions
32 lines (25 loc) · 884 Bytes
/
letterCombinations.py
File metadata and controls
32 lines (25 loc) · 884 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution:
def letterCombinations(self, digits):
res = []
if not digits:
return []
digits_to_letters = {
'2': 'abc',
'3': 'def',
'4': 'ghi',
'5': 'jkl',
'6': 'mno',
'7': 'pqrs',
'8': 'tuv',
'9': 'wxyz',
}
def backtrack(idx, comb):
if idx == len(digits):
res.append(comb[:])
return
for letter in digits_to_letters[digits[idx]]:
backtrack(idx+1, comb+letter)
backtrack(0, '')
return res
myVar = Solution()
myVar.letterCombinations("23")