one line of code at a time
[leetcode] 17. Letter Combinations of a Phone Number 파이썬 코드 본문
https://leetcode.com/problems/letter-combinations-of-a-phone-number/
각 숫자마다 designated letter들이 있다.
digits가 "23"일 때 2에 있는 문자 a, b, c와 3에 있는 문자 d, e, f의 조합으로 몇 개의 letter combination을 만들 수 있는지 구하는 문제다.
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
res = []
digitsToChar = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz"
}
def dfs(i, currStr):
if i == len(digits):
res.append(currStr)
return True
for c in digitsToChar[digits[i]]:
dfs(i+1, currStr+c)
if digits:
dfs(0, "")
return res

'leetcode' 카테고리의 다른 글
| [leetcode] 141. Linked List Cycle 파이썬 코드 (0) | 2024.09.25 |
|---|---|
| [leetcode] 841. Keys and Rooms 파이썬 코드 (0) | 2024.09.24 |
| [leetcode] 463. Island Perimeter 파이썬 코드 (0) | 2024.09.24 |
| [leetcode] 79. Word Search 파이썬 코드 (0) | 2024.09.24 |
| [leetcode] 802. Find Eventual Safe States 파이썬 코드 (2) | 2024.09.23 |