-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17_Letter_Combination_of_a_phone.py
44 lines (36 loc) · 1.15 KB
/
17_Letter_Combination_of_a_phone.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
42
43
44
from collections import defaultdict
import string
from itertools import product
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
if not digits:
return None
alpha = list(string.ascii_lowercase)
look = []
for i in digits:
temp = []
if int(i) < 7:
index = (int(i) - 2) * 3
for t in range(3):
temp.append(alpha[index + t])
elif int(i) == 7:
index = (int(i) - 2) * 3
for t in range(4):
temp.append(alpha[index + t])
elif int(i) == 8:
index = (int(i) - 2) * 3 + 1
for t in range(3):
temp.append(alpha[index + t])
else:
index = (int(i) - 2) * 3 + 1
for t in range(4):
temp.append(alpha[index + t])
look.append(temp)
result = product(*look)
output = []
for item in list(result):
word = ''
for s in item:
word += s
output.append(word)
return output