-
Notifications
You must be signed in to change notification settings - Fork 305
/
17.py
39 lines (31 loc) · 984 Bytes
/
17.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
class Solution(object):
def letterCombinations(self, string):
# input : "23"
# output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
#Edge
if not string:
return []
#Global Variable
self.dict = {
"2":"abc",
"3":"def",
"4":"ghi",
"5":"jkl",
"6":"mno",
"7":"pqrs",
"8":"tuv",
"9":"wxyz"
}
self.res = []
# Helper function
def dfs(string, index, temp):
if len(temp) == len(string):
self.res.append("".join(x for x in temp))
return
for char in self.dict[string[index]]:
temp.append(char)
dfs(string, index+1, temp)
temp.pop()
# Function Call
dfs(string, 0, [])
return self.res