Appearance
Approach: DP where dp[i][j] means s3[:i+j] can be formed from s1[:i] and s2[:j]; transition from left (take s2) or up (take s1) when the next char matches.
Complexity: O(mn) time, O(mn) space
go
func isInterleave(s1 string, s2 string, s3 string) bool {
m, n := len(s1), len(s2)
if m+n != len(s3) {
return false
}
dp := make([][]bool, m+1)
for i := range dp {
dp[i] = make([]bool, n+1)
}
dp[0][0] = true
for i := 0; i <= m; i++ {
for j := 0; j <= n; j++ {
if i > 0 && dp[i-1][j] && s1[i-1] == s3[i+j-1] {
dp[i][j] = true
}
if j > 0 && dp[i][j-1] && s2[j-1] == s3[i+j-1] {
dp[i][j] = true
}
}
}
return dp[m][n]
}