- Reference
print(element + c)
λ₯Ό μ°μ΄ λ΄μΌ μ΄ν΄ν μ μλ λ‘μ§
Solution
from typing import List
class Solution:
# μ‘°ν© λ¬Έμ λ μμ κ³ λ € X
def letterCombinations(self, digits: str) -> List[str]:
answer = []
# '1'μ΄λ '0'μ λ§λ€μ΄μ€ νμκ° μμ (μμΈ μ²λ¦¬ μ ν΄μ€λ λλλ‘ λ¬Έμ μμ μ μ)
digits_map = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}
# (0 <= digits.length <= 4) -> μ¬κ·λ‘ νΈλ κ² λ ν¨κ³Όμ μΌ λ―
def dfs(index: int, element: str) -> None:
# λκΉμ§ νμ -> λ°±νΈλνΉ
if len(digits) == len(element):
answer.append(element)
return
for i in range(index, len(digits)):
# ν μ리μμ μ«μ(ex. digits_map['2'])μ λν string(ex. 'abc')
for c in digits_map[digits[i]]:
print(element + c)
dfs(i + 1, element + c)
if not digits:
return []
dfs(0, '')
return answer
Leave a comment