일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |
Tags
- FIT XR
- 뭐든
- Problem Solving
- 운동
- Writing
- 3줄정리
- 30분
- 리얼 클래스
- 월간
- 스탭퍼
- realclass
- 10분
- 읽기
- 파비최
- English
- 개발자
- 매일
- 사이드
- 프로젝트
- 영어공부
- Daily Challenge
- 만화도
- 영어원서읽기
- leetcode
- 화상영어
- 링피트
- 쓰릴오브파이트
- 괜찮음
- 미드시청
- 잡생각
Archives
- Today
- Total
파비의 매일매일 공부기록
Today's Challenge 본문
https://leetcode.com/problems/interleaving-string/
Interleaving String - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Medium인데 어렵네... 몇 번 실패하고 결국 솔루션 봄.
결국 DFS로 풀어야 되네.
처음에 내 아이디어는 뒤에서부터 하나 씩 idx를 줄여가는 거였는데
그러면 분기되는 부분이 많아서 답을 찾기는 힘들 듯. 전체 탐색을 해야 하는구먼..
class Solution:
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
if len(s1) + len(s2) != len(s3):
return False
@cache
def dfs(i, j):
if i == len(s1) and j == len(s2):
return True
cs1, cs2 = False, False
if i < len(s1) and s1[i] == s3[i + j]:
cs1 = dfs(i + 1, j)
if j < len(s2) and s2[j] == s3[i + j]:
cs2 = dfs(i, j + 1)
return cs1 or cs2
return dfs(0, 0)
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
Today's Challenge (0) | 2022.07.09 |
---|---|
Today's Challenge (0) | 2022.07.08 |
Today's Challenge (0) | 2022.07.06 |
Today's Challenge (0) | 2022.07.05 |
Today's Challenge (0) | 2022.07.04 |
Comments