The O(n) Club: Interleaving String — Your DP Soap Opera Starter Pack
⚡ TL;DR
Check if
s3
is a flawless mashup ofs1
ands2
, but don’t mess with their order or you’ll trigger a soap opera-worthy meltdown. Brute force is like sorting your laundry by hand in a hurricane—use Dynamic Programming for sanity. Here’s a speedy Java snapshot:// Is s3 an interleaving of s1 and s2? boolean isInterleave(String s1, String s2, String s3) { if (s1.length() + s2.length() != s3.length()) return false; boolean[] dp = new boolean[s2.length() + 1]; dp[0] = true; for (int i = 0; i <= s1.length(); i++) { for (int j = 0; j <= s2.length(); j++) { if (i == 0 && j == 0) continue; if (i > 0) dp[j] = dp[j] && s1.charAt(i - 1) == s3.charAt(i + j - 1); if (j > 0) dp[j] = dp[j] || (dp[j - 1] && s2.charAt(j - 1) == s3.charAt(i + j - 1)); } } return dp[s2.length()]; }
🧠 How Devs Usually Mess This Up
Lots of people think interleaving is just alternating picks—like passing notes in class or letting two toddlers fight over LEGO bricks. Nope. Greedy doesn’t work. If you try to swap characters around or ignore the original order, the interleaving police (and your code) will destroy you. Anyone skipping the quick length sanity check is also in for a dramatic runtime performance (and by performance, I mean TLE on LeetCode).
🔍 What’s Actually Going On
Imagine s1
and s2
are two actors reading from a script, and s3
is the improv show. Each actor must say all their lines, in order, but it’s up to you (the algorithmic director) to intermingle their dialogue without dropping or reordering anything. The job: for every prefix pair (number of lines delivered), did the performance reach this point flawlessly? Dynamic Programming tracks these standing ovations, across all possible script progressions.
🛠️ PseudoCode
- Step 1: Quick Bailout
if (s1.length + s2.length != s3.length) return false
Basically, don’t let the wrong number of actors on stage. - Step 2: DP Array Setup
boolean[] dp = new boolean[s2.length + 1];
dp[j] = can we reach prefix of length i+j by using i from s1, j from s2? - Step 3: Fill the Array
for i from 0 to s1.length:<br> for j from 0 to s2.length:
Loop through every possible drama configuration. - Step 4: The Core Check
if (i > 0) dp[j] = dp[j] && (s1.charAt(i - 1) == s3.charAt(i + j - 1));<br>if (j > 0) dp[j] = dp[j] || (dp[j - 1] && (s2.charAt(j - 1) == s3.charAt(i + j - 1));
Is the next actor’s line the right one? If yes, keep the applause going. - Step 5: Curtain Call
return dp[s2.length];
Did we survive the whole performance with no dramatic slip-ups?
💻 The Code
public boolean isInterleave(String s1, String s2, String s3) {
int m = s1.length(), n = s2.length();
if (m + n != s3.length()) return false;
boolean[] dp = new boolean[n + 1];
dp[0] = true;
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 && j == 0) continue;
if (i > 0) dp[j] = dp[j] && s1.charAt(i - 1) == s3.charAt(i + j - 1);
if (j > 0) dp[j] = dp[j] || (dp[j - 1] && s2.charAt(j - 1) == s3.charAt(i + j - 1));
}
}
return dp[n];
}
⚠️ Pitfalls, Traps & Runtime Smacks
- Order matters — no shuffling allowed, unless you want runtime heartbreak.
- Skip the length check? Debug purgatory and mysterious failures await you.
- Empty strings are totally valid. Don’t freak out—DP handles them like a pro.
- All inputs same letter? Still works. It just won’t win a Best Drama award.
- Space: O(n), Time: O(mn). For once, you actually optimize both!
🧠 Interviewer Brain-Tattoo
“Interleaving strings isn’t just a puzzle—it’s a lesson: you can’t always improvise your way past the rules.”