-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17_medium_[dfs]_letter_combination_of_phone_number.py
53 lines (41 loc) · 1.48 KB
/
17_medium_[dfs]_letter_combination_of_phone_number.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
45
46
47
48
49
50
51
52
53
from typing import List
class Solution:
def _dfs(self, digits, depth, candidate, digit_to_alphabet_list, answer):
if depth == len(digits):
answer.append(candidate)
return
current_digit = digits[depth]
for alphabet in digit_to_alphabet_list[current_digit]:
self._dfs(digits, depth+1, candidate+alphabet, digit_to_alphabet_list, answer)
def letterCombinations(self, digits: str) -> List[str]:
if digits == '':
return []
digit_to_alphabet_list = {
'2': ['a', 'b', 'c'],
'3': ['d', 'e', 'f'],
'4': ['g', 'h', 'i'],
'5': ['j', 'k', 'l'],
'6': ['m', 'n', 'o'],
'7': ['p', 'q', 'r', 's'],
'8': ['t', 'u', 'v'],
'9': ['w', 'x', 'y', 'z']
}
answer = []
self._dfs(digits, 0, '', digit_to_alphabet_list, answer)
return answer
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
MAPPING = ('0', '1', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz')
res = ['']
for d in digits:
new_res = []
for pre in res:
for cur in MAPPING[int(d)]:
new_res.append(pre + cur)
res = new_res
if len(digits) == 0:
return []
return res
if __name__ == '__main__':
print(Solution().letterCombinations('23'))
print(Solution().letterCombinations(''))