Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Lines changed: 21 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /