-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
32 lines (29 loc) · 791 Bytes
/
solution.py
File metadata and controls
32 lines (29 loc) · 791 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(object):
def __init__(self):
self.digits_map = [
'abc',
'def',
'ghi',
'jkl',
'mno',
'pqrs',
'tuv',
'wxyz'
]
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
res = []
if digits != '':
self.backtrack(0, digits, '', res)
return res
def backtrack(self, beg, digits, temp, res):
if beg == len(digits):
res.append(temp)
else:
characters = self.digits_map[int(digits[beg]) - 2]
for c in characters:
t = temp + c
self.backtrack(beg + 1, digits, t, res)