# Interleaving String 1D DP (Practice 5046): Sketch

Interviewers use this pattern to test whether you can state the invariant before coding. Here is a compact rehearsal note.

## Approach

dp[j] is true when s1[:i] and s2[:j] can interleave into s3[:i+j].

```python
def is_interleave(a,b,c):
    if len(a)+len(b)!=len(c): return False
    dp=[True]+[False]*len(b)
    for j in range(1,len(b)+1): dp[j]=dp[j-1] and b[j-1]==c[j-1]
    for i in range(1,len(a)+1):
        dp[0]=dp[0] and a[i-1]==c[i-1]
        for j in range(1,len(b)+1):
            dp[j]=(dp[j] and a[i-1]==c[i+j-1]) or (dp[j-1] and b[j-1]==c[i+j-1])
    return dp[-1]
```

## Complexity

Linear in the dominant input size for the scan; heap/tree variants add a log factor.

## Interview pitfalls

State the invariant out loud, name the complexity, then code. Skipping the invariant is the usual fail mode.

## Practicing out loud

Narrate the approach, complexity and one edge case in under a minute. Disclosure: I work on Linkjob AI, a real-time interview assistant. If you want live practice help, see [linkjob.ai](https://www.linkjob.ai/).