The Trick to Solve Minimum Window Subsequence
44sReveals a clever two-pointer DP trick that simplifies a hard problem, appealing to coders looking for efficient solutions.
▶ Play Clip"Delivers a solid explanation of the algorithm, though the title's 'Hard' may oversell the simplicity of the presented approach."
This video explains how to solve the 'Minimum Window Subsequence' problem, a hard dynamic programming challenge. The presenter demonstrates an efficient two-pointer approach that finds the shortest substring in S1 containing S2 as a subsequence, reducing time complexity from cubic to O(M*N).
The goal is to find the shortest window inside S1 that contains S2 as a subsequence, meaning letters of S2 must appear in S1 in order but not necessarily adjacent.
The brute force approach tries every substring of S1 and checks if S2 fits inside, resulting in cubic time complexity, which is too slow.
Use two pointers: pointer I walks through S1, and pointer J tracks how far we've matched into S2. When S1[I] equals S2[J], we make progress on a partial match.
A DP array stores the index in S1 where the match for S2 up to position J begins. This remembers where the current match started.
Case 1: When the first letter of S2 matches, start a fresh window (DP[0] = I). Case 2: When a later letter matches, inherit the start from the previous step (DP[J] = DP[J-1]).
Scan S1, mark the start on first match, extend the match on subsequent matches, and carry the start forward. When the full match completes, compute window length as I - DP[end] + 1.
After finding a complete match, continue scanning because a later starting point could yield a shorter window. The algorithm does one pass through S1 with constant work per character.
Total time complexity is O(M*N), and the shortest valid window is found. For more breakdowns, visit hellointerview.com (link in bio).
The video presents a clear, efficient solution to the Minimum Window Subsequence problem using a DP array and two pointers, achieving O(M*N) time complexity. It emphasizes the importance of continuing the scan to ensure the shortest window is found.
What is the problem statement for Minimum Window Subsequence?
Find the shortest window in S1 that contains S2 as a subsequence, meaning letters of S2 appear in S1 in order but not necessarily adjacent.
00:01
What is the time complexity of the brute force approach?
Cubic time, O(N^3).
00:14
What does the DP array store in this algorithm?
DP[J] stores the index in S1 where the match for S2 up to position J begins.
00:41
What are the two cases in the DP transition?
Case 1: When the first letter of S2 matches, set DP[0] = I. Case 2: When a later letter matches, set DP[J] = DP[J-1].
00:57
How do you compute the window length when a full match is found?
Window length = I - DP[end] + 1.
01:12
Why do you continue scanning after finding a complete match?
Because a later starting point could give a shorter window.
01:26
What is the overall time complexity of the optimal algorithm?
O(M*N), where M is length of S1 and N is length of S2.
01:41
Two-Pointer Technique
Introduces an efficient alternative to brute force, reducing complexity significantly.
00:27DP Array for Start Indices
Clever use of DP to track the start of matches, enabling O(1) window length calculation.
00:41Continuing Scan for Shorter Windows
Emphasizes that the first complete match may not be optimal, a key insight for correctness.
01:26[00:01] problem, but once you see the trick, it actually becomes pretty easy. See, to find the shortest window inside S1 that contains S2 as a subsequence. So, the letters of S2 must appear in S1 in
[00:14] order, but they do not have to be next to each other. The brute force is to try every substring of S1 and to check if S2 fits inside. That's cubic time, which is of course way too slow. So, here's the better idea. We use two pointers.
[00:27] Pointer I walks through S1 and pointer J tracks how far we have matched into S2. Every time S1 at I equals S2 at J, we make a little progress on a partial match. We only need to remember one thing, where the match started. So, DP
[00:41] of J stores the index in S1 where our match for S2 up to position J begin. There are only two cases. Case one is when the first letter of S2 matches, we begin a fresh window. DP at zero becomes I. Case two is when a later letter of S2
[00:57] matches, we inherit the start from the step before. DP at J equals DP at J minus one. The same window that built S2 up to J minus one now reaches one letter further. Let us trace it. We scan S1 and hit our first B. Mark the start, then a
[01:12] D, which extends the match. So, we carry the start forward. Then we reach E and the full match completes. The window length is just I minus DP at the end plus one. We have this as our best window, but we don't stop here. A later
[01:26] starting point could give us a shorter window. So, we keep scanning, and we seen. That's the whole algorithm. One pass through S1 with constant work per character. The total time is O of M times N, and the shortest valid window
[01:41] breakdowns, head over to hellointerview.com. Link is in our bio.
⚡ Saved you 0h 01m reading this? Transcribe any YouTube video for free — no signup needed.