반응형
leetcode.com/problems/letter-combinations-of-a-phone-number/
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
dict ={
'2':'abc',
'3':'def',
'4':'ghi',
'5':'jkl',
'6':'mno',
'7':'pqrs',
'8':'tuv',
'9':'wxyz'
}
if digits =="":
return []
digits_list = []
for i in range(len(digits)):
digits_list.append(digits[i])
output =[]
def combineNext(letter='',i=0):
if len(digits_list)>i:
digit= digits_list[i]
for le in dict[digit]:
k = letter + le
combineNext(k,i+1)
else:
output.append(letter)
combineNext()
return output
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
if not digits:
return ''
table={
'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'],
}
result=[]
def dfs(word, idx):
if idx < len(digits):
char_list = table[digits[idx]]
for char in char_list:
dfs(word+char, idx+1)
else:
result.append(word)
dfs('',0)
return result
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
if not digits:
return ''
table={
'2':'abc',
'3':'def',
'4':'ghi',
'5':'jkl',
'6':'mno',
'7':'pqrs',
'8':'tuv',
'9':'wxyz',
}
result=[]
def dfs(word, idx):
if idx == len(digits):
result.append(word)
return
for char in table[digits[idx]]:
dfs(word+char, idx+1)
dfs('',0)
return result
댓글