← Recursion & DP

Micro-Drill #132 — Interleaving string check

Recursion & DP Target: 15s

2D DP checks if characters from s1 and s2 interleave to form s3.

dp[0][0] = True
for i in range(m+1):
    for j in range(n+1):
        if i and s1[i-1] == s3[i+j-1]: dp[i][j] |= dp[i-1][j]
        if j and s2[j-1] == s3[i+j-1]: dp[i][j] |= dp[i][j-1]

Type it from memory. Go.

Practice Problems

Related Coding Drills

← Micro #131 Micro #133 →