-
Notifications
You must be signed in to change notification settings - Fork 19
/
answer.py
26 lines (21 loc) · 778 Bytes
/
answer.py
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
#!/usr/bin/python
#------------------------------------------------------------------------------
class Solution:
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
if len(digits) == 0: return []
return self.helper("", digits, [])
def helper(self, prefix, digits, result):
mapping = ["0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
if len(digits) == 0:
result.append(prefix)
return result
chars = mapping[int(digits[0])]
for c in chars:
self.helper(prefix+c, digits[1:], result)
return result
#------------------------------------------------------------------------------
#Testing