Skip to content

Commit 038f39e

Browse files
Update and rename interleaving_string.cpp to interleaving_string.py
1 parent ec6dbaf commit 038f39e

File tree

2 files changed

+16
-31
lines changed

2 files changed

+16
-31
lines changed

interleaving_string.cpp

-31
This file was deleted.

interleaving_string.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
3+
if len(s1) + len(s2) != len(s3): return False
4+
m, n = len(s1), len(s2)
5+
6+
@lru_cache(None)
7+
def dp(i, j):
8+
if i == m and j == n: return True # Found a valid match
9+
ans = False
10+
if i < m and s1[i] == s3[i+j]: # Case match s1[i] with s3[i+j]
11+
ans |= dp(i + 1, j)
12+
if j < n and s2[j] == s3[i+j]: # Case match s2[j] with s3[i+j]
13+
ans |= dp(i, j + 1)
14+
return ans
15+
16+
return dp(0, 0)

0 commit comments

Comments
 (0)