Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 빈 조회 2개 이상
- 항해99
- 디자인 패턴
- 단기개발자코스
- 개발자부트캠프추천
- 프로그래머스
- 빈 충돌
- 인프콘 2024
- jwt
- 99클럽
- 프로그래머스 이중우선순위큐
- 전략패턴 #StrategyPattern #디자인패턴
- KPT회고
- TiL
- 디자인패턴
- Python
- 개발자 취업
- jwttoken
- JavaScript
- DesignPattern
- 코딩테스트 준비
- 구글 OAuth login
- 커스텀 헤더
- spring batch 5.0
- 파이썬
- infcon 2024
- 1주일회고
- @FeignClient
- Spring multimodule
- 취업리부트코스
Archives
- Today
- Total
m1ndy5's coding blog
LeetCode 17. Letter Combinations of a Phone Number with Python 본문
알고리즘 with python/알고리즘 스터디
LeetCode 17. Letter Combinations of a Phone Number with Python
정민됴 2024. 1. 10. 09:57https://leetcode.com/problems/letter-combinations-of-a-phone-number/
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
dic = {'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']}
if digits == "":
return []
key = digits[0]
# key를 2, 23, 235 이런식으로 붙여서 다시 dic에 저장
for i in range(1, len(digits)):
values = []
for a1 in dic[key]:
for a2 in dic[digits[i]]:
values.append(a1+a2)
key += digits[i]
dic[key] = values
return dic[key]
책코드
def letterCombinations(self, digits):
# digits의 자리수만큼 가능한 조합 다찾기
def dfs(index, path):
if len(path) == len(digits):
result.append(path)
return
for i in range(idex, len(digits)):
for j in dic[digits[i]]:
dfs(i+1, path+j)
if not digits:
return []
dic = {"2" :"abc", "3": "def", "4": "ghi", "5": "jkl", "6": "mno", "7":"pqrs", "8":"tuv", "9": "wxyxz"}
result = []
dfs(0, "")
return result
'알고리즘 with python > 알고리즘 스터디' 카테고리의 다른 글
LeetCode 77. Combinations(조합) with Python (0) | 2024.01.10 |
---|---|
LeetCode 46.Permutations(순열) with Python (0) | 2024.01.10 |
LeetCode 328. Odd Even Linked List with Python (0) | 2024.01.06 |
LeetCode 21. Merge Two Sorted Lists with Python (0) | 2024.01.06 |
LeetCode 206.Reverse Linked List with Python (0) | 2024.01.06 |