#97
Interleaving String
medium· 2-D DPruns: 0Given strings s1, s2, and s3, find whether s3 is formed by an interleaving of s1 and s2. An interleaving is formed by splitting each string into non-empty substrings and concatenating the pieces in an alternating order, while preserving the relative order of characters within each original string.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 572
class Solution:
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
if len(s1) + len(s2) != len(s3):
return False
m, n = len(s1), len(s2)
dp = [[False] * (n + 1) for _ in range(m + 1)]
dp[0][0] = True
for i in range(m + 1):
for j in range(n + 1):
if i > 0 and s1[i - 1] == s3[i + j - 1]:
dp[i][j] = dp[i][j] or dp[i - 1][j]
if j > 0 and s2[j - 1] == s3[i + j - 1]:
dp[i][j] = dp[i][j] or dp[i][j - 1]
return dp[m][n]
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.