-
Notifications
You must be signed in to change notification settings - Fork 20
/
LetterCombinationsOfAPhoneNumber.py
41 lines (34 loc) · 1.2 KB
/
LetterCombinationsOfAPhoneNumber.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Given a digit string, return all possible letter combinations that the number could represent.
#
# A mapping of digit to letters (just like on the telephone buttons) is given below.
#
# Input:Digit string "23"
# Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
# Note:
# Although the above answer is in lexicographical order, your answer could be in any order you want.
#
# Python, Python3 all accepted.
class LetterCombinationsOfAPhoneNumber:
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
t = (" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz")
results = []
digits_length = len(digits)
if digits_length == 0:
return results
if digits_length == 1:
for c in t[int(digits)]:
results.append(str(c))
return results
li = self.letterCombinations(str(digits[1:digits_length]))
builder = ""
for c in t[int(str(digits[0:1]))]:
for s in li:
builder += str(c)
builder += s
results.append(builder)
builder = ""
return results