◀ THE GRIND — 2-D DYNAMIC PROGRAMMING

Interleaving String

MEDIUM✓ CHIP-TIMEDLC #97 — FULL STATEMENT ↗

The drill: Two source strings and a candidate merge — decide whether the candidate could have been built by interleaving the two sources while keeping each source's own character order intact.

THE BRIEFING — THE FULL DRILL, IN MY OWN WORDS

Two source strings and a third candidate string arrive, and the task is to decide whether the candidate could have been built by interleaving the two sources together — weaving their characters into one sequence while keeping each source's own internal character order untouched.

Characters from the two sources can be interleaved in any pattern, switching back and forth as often as needed, but a source's own characters always have to appear in the same order they started in. Every character from both sources has to be used exactly once, with nothing left over.

The output is just yes or no — whether at least one valid interleaving produces the candidate string.

EX 01
s1 = "" · s2 = "" · s3 = ""
true
EVERYTHING EMPTY
EX 02
s1 = "a" · s2 = "" · s3 = "a"
true
SECOND SOURCE CONTRIBUTES NOTHING
EX 03
s1 = "" · s2 = "b" · s3 = "b"
true
FIRST SOURCE CONTRIBUTES NOTHING
THE HINTS — TAKE ONLY WHAT YOU NEED
HINT 1 THE NUDGE

If the candidate isn't even the right length, it can't be an interleaving — check that first. Otherwise, having consumed i characters from one source and j from the other always points at one exact position in the candidate: i + j.

HINT 2 THE STRUCTURE

At every step, the next character of the candidate has to come from the next unused character of source one or the next unused character of source two — never from further ahead in either.

HINT 3 ONE STEP FROM THE ANSWER

DP: dp[i][j] = true when the first i+j characters of the candidate can be built from the first i of s1 and first j of s2. It's true if (dp[i-1][j] and s1[i-1] matches) or (dp[i][j-1] and s2[j-1] matches).

COACH'S BOARD — THE PATTERN, STEP BY STEP
THE WEAVE CHECKPATTERN · GRID DP — TWO SOURCESs1 = "ab" · s2 = "cd" · s3 = "acbd"
T
STEP 1

s1='ab', s2='cd', s3='acbd'. dp[i][j] asks: can the first i+j chars of acbd come from the first i of s1 and first j of s2? dp[0][0]=true trivially.

STEP 1 / 9 · ← → WORK TOO
THE SPLITS — TWO PACES, TWO LANGUAGES
grind/interleaving-string.pyRACE PACE
LANG ▸
PACE ▸
class Solution:
    def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
        m, n = len(s1), len(s2)
        if m + n != len(s3):
            return False
        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 dp[i - 1][j] and s1[i - 1] == s3[i + j - 1]:
                    dp[i][j] = True
                if j > 0 and dp[i][j - 1] and s2[j - 1] == s3[i + j - 1]:
                    dp[i][j] = True
        return dp[m][n]
TIME O(M·N)SPACE O(M·N)PYTHON · RACE PACE · 14 LN

✓ CHIP-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED