Skip to content

Commit e671351

Browse files
Create Solution.py
1 parent f4027ae commit e671351

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Interleaving String/Solution.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
3+
if len(s1) + len(s2) != len(s3):
4+
return False
5+
6+
m = len(s1)
7+
n = len(s2)
8+
9+
dp = [[False] * (n+1) for _ in range(m+1)]
10+
11+
dp[0][0] = True
12+
for i in range(1, m+1):
13+
dp[i][0] = dp[i-1][0] and s3[i-1] == s1[i-1]
14+
for j in range(1, n+1):
15+
dp[0][j] = dp[0][j-1] and s3[j-1] == s2[j-1]
16+
17+
for i in range(1, m+1):
18+
for j in range(1, n+1):
19+
dp[i][j] = (dp[i-1][j] and s3[i+j-1]==s1[i-1]) or (dp[i][j-1] and s3[i+j-1]==s2[j-1])
20+
21+
return dp[-1][-1]

0 commit comments

Comments
 (0)