Data Structures & Algorithms Dynamic Programming — 2D
💡
Exercise 134

Interleaving String 30 XP Hard

LeetCode Ctrl+Enter Run Ctrl+S Save

Given s1, s2, s3, check if s3 is an interleaving of s1 and s2. Characters from s1 and s2 appear in s3 in their relative order.

2D DP: dp[i][j] = True if s3[:i+j] can be formed by interleaving s1[:i] and s2[:j].

💡 Pro tip: Understand this problem deeply — don't just memorize the code. Try explaining the approach out loud as if teaching a friend. If you can explain it simply, you truly understand it!

📋 Instructions
Check if 'aadbbcbcac' is an interleaving of 'aabcc' and 'dbbca'.
dp[i][j] = (dp[i-1][j] AND s1[i-1]==s3[i+j-1]) OR (dp[i][j-1] AND s2[j-1]==s3[i+j-1]).
🧪 Test Cases
Input
isInterleave('aabcc', 'dbbca', 'aadbbcbcac')
Expected
True
Boolean check
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.