-
Notifications
You must be signed in to change notification settings - Fork 0
/
17.letter-combinations-of-a-phone-number.py
43 lines (36 loc) · 1.3 KB
/
17.letter-combinations-of-a-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
#
# @lc app=leetcode id=17 lang=python3
#
# [17] Letter Combinations of a Phone Number
#
# @lc code=start
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
def comb(dict, digits, index, path, res):
if index >= len(nums):
res.append(path)
return
string1 = dict[digits[index]]
for i in string1:
comb(dict, digits, index+1, path + i, res)
dict = {"2": "abc", "3": "def", "4": "ghi", "5": "jkl", "6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz"}
results = []
if len(digits) == 0:
return results
comb(dict, digits, 0, '', results)
return results
def letterCombinations2(self, digits: str) -> List[str]:
if len(digits) == 0:
return []
dict = {"2": "abc", "3": "def", "4": "ghi", "5": "jkl", "6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz"}
results = []
def comb(digits, index, path):
if len(path) == len(digits):
results.append(path)
return
characters = dict[digits[index]]
for character in characters:
comb(digits, index+1, path+character)
comb(digits, 0, '')
return results
# @lc code=end