MEDIUM NC#116 1-D Dynamic Programming
97. Interleaving String
📖 Problem
Given strings s1, s2, and s3, find whether s3 is formed by an interleaving of s1 and s2.
🧠 Visual Learning Aid
1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases
JS/TS Refreshers
- •Array methods (`push`, `pop`, `shift`, `slice`)
- •Object/Map/Set usage patterns
- •Function parameter and return typing
- •Array DP table updates
- •State transition thinking
- •Base case initialization
Logical Thinking Concepts
- •Define invariants before coding
- •Check edge cases first (`[]`, single element, duplicates)
- •Estimate time/space before implementation
- •Apply Dynamic Programming reasoning pattern
💡 Approach
- → Use dp[i][j] = true if s3[0:i+j] = s1[i] + s2[j]
- → If indices reach end: dp[m][n] means we've used all chars from both strings
- → Time: O(m * n * p), Space: O(m * n * p)
🧭 Prerequisites
🛠️ Hints & Pitfalls
Hints
- •Use dp[i][j] = true if s3[0:i+j] = s1[i] + s2[j]
- •If indices reach end: dp[m][n] means we've used all chars from both strings
- •Time: O(m * n * p), Space: O(m * n * p)
🧪 Test Cases
Test Case 1
Not run Input:
isInterleave(s1: string, s2: string, s3: string); Expected:
Computed from hidden reference Test Case 2
Not run Input:
isInterleave('aab', 'axy', 'zaz'); Expected:
Computed from hidden reference Test Case 3
Not run Input:
isInterleave('', '', ''); Expected:
Computed from hidden reference 📝 Code Editor
📤 Output