97. Interleaving String
2025. 10. 14. 21:05ㆍAlgorithm/Leetcode, Lintcode, HackerRank, etc.
- 목차
반응형
class Solution:
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
if len(s1) + len(s2) > len(s3):
return False
def dfs(i, j, merged):
if (i, j) in mem:
return mem[(i, j)]
if merged and merged[-1] != s3[len(merged) - 1]:
return False
if i == len(s1) and j == len(s2):
return s3 == merged
res = False
if i < len(s1):
res = res or dfs(i + 1, j, merged + s1[i])
if j < len(s2):
res = res or dfs(i, j + 1, merged + s2[j])
mem[(i, j)] = res
return res
mem = {}
return dfs(0, 0, '')
반응형
'Algorithm > Leetcode, Lintcode, HackerRank, etc.' 카테고리의 다른 글
2806. Account Balance After Rounded Purchase (0) | 2025.10.13 |
---|---|
2525. Categorize Box According to Criteria (0) | 2025.10.13 |
2933. High-Access Employees (0) | 2025.10.13 |
3446. Sort Matrix by Diagonals (0) | 2025.10.07 |
884. Uncommon Words from Two Sentences (0) | 2025.10.07 |