Letter Combinations of a Phone Number
Python
1from itertools import product
2class Solution:
3 def get_letters(self, x):
4 if x == 2:
5 return list("abc")
6 elif x == 3:
7 return list("def")
8 elif x == 4:
9 return list("ghi")
10 elif x == 5:
11 return list("jkl")
12 elif x == 6:
13 return list("mno")
14 elif x == 7:
15 return list("pqrs")
16 elif x == 8:
17 return list("tuv")
18 elif x == 9:
19 return list("wxyz")
20
21 def letterCombinations(self, digits):
22 """
23 :type digits: str
24 :rtype: List[str]
25 """
26 if digits == "":
27 return []
28 numbers = list(digits)
29 all_letters = []
30 ans = []
31 for num in numbers:
32 temp = self.get_letters(int(num))
33 all_letters.append(temp)
34 combo = list(product(*all_letters))
35 for comb in combo:
36 ans.append(''.join(comb))
37 return ans