0%

17. Letter Combinations of a Phone Number

https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/

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
class Solution:
def letterCombinations(self, A: str) -> List[str]:
D={
'2': "abc",
'3': 'def',
'4': 'ghi',
'5': 'jkl',
'6': 'mno',
'7': 'pqrs',
'8': 'tuv',
'9': 'wxyz'
}
def f(idx,curr):
if len(curr)==len(A):
Z.append("".join(curr))
return
candidates=D[A[idx]]
for candidate in candidates:
curr.append(candidate)
f(idx+1,curr)
curr.pop()
Z=[]
if not A:
return []
f(0,[])
return Z