Shubham Sourabhthe room · director's cutplain résumé
Room 04 · The Grind — 2-D Dynamic Programming

Interleaving String

MEDIUM✓ CHIP-TIMEDLC #97 — FULL STATEMENT

The briefingthe full drill, in my own words

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.

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.

  • the candidate's length must equal the combined length of both sources
  • strings stay short enough for an O(m·n) table
  • characters from each source keep their original relative order
  • the answer is a single true/false
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 hintstake 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 boardthe 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 splitstwo paces, two languages

grind/interleaving-string.py — RACE 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-TIMEDALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED