Bird
0
0
DSA Cprogramming~10 mins

Minimum Window Substring in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Minimum Window Substring
Start
Initialize frequency map of target chars
Set two pointers: left=0, right=0
Expand right pointer to include chars
Update window counts and check if window valid
While window valid: try to shrink from left
Update minimum window if smaller
Move left pointer to shrink window
Repeat expand/shrink until right reaches end
Return minimum window substring or empty if none
We move two pointers over the string to find the smallest substring containing all target chars, expanding right to include chars and shrinking left to minimize.
Execution Sample
DSA C
s = "ADOBECODEBANC";
t = "ABC";
// Find min window in s containing all chars of t
Find the smallest substring in s that contains all characters from t.
Execution Table
StepOperationLeft PointerRight PointerWindow CountsValid CountMinimum WindowVisual State
1Initialize frequency map for t00{"A":0,"B":0,"C":0}0null""
2Expand right to 'A'00{"A":1,"B":0,"C":0}1null[A]
3Expand right to 'D'01{"A":1,"B":0,"C":0}1null[A D]
4Expand right to 'O'02{"A":1,"B":0,"C":0}1null[A D O]
5Expand right to 'B'03{"A":1,"B":1,"C":0}2null[A D O B]
6Expand right to 'E'04{"A":1,"B":1,"C":0}2null[A D O B E]
7Expand right to 'C'05{"A":1,"B":1,"C":1}3Window 'ADOBEC'[A D O B E C]
8Shrink left from 'A'15{"A":0,"B":1,"C":1}2Window 'ADOBEC'[D O B E C]
9Expand right to 'O'16{"A":0,"B":1,"C":1}2Window 'ADOBEC'[D O B E C O]
10Expand right to 'D'17{"A":0,"B":1,"C":1}2Window 'ADOBEC'[D O B E C O D]
11Expand right to 'E'18{"A":0,"B":1,"C":1}2Window 'ADOBEC'[D O B E C O D E]
12Expand right to 'B'19{"A":0,"B":2,"C":1}2Window 'ADOBEC'[D O B E C O D E B]
13Expand right to 'A'110{"A":1,"B":2,"C":1}3Window 'ADOBEC'[D O B E C O D E B A]
14Shrink left from 'D'210{"A":1,"B":2,"C":1}3Window 'DOBECODEBA'[O B E C O D E B A]
15Shrink left from 'O'310{"A":1,"B":2,"C":1}3Window 'OBECODEBA'[B E C O D E B A]
16Shrink left from 'B'410{"A":1,"B":1,"C":1}3Window 'BECODEBA'[E C O D E B A]
17Shrink left from 'E'510{"A":1,"B":1,"C":1}3Window 'ECODEBA'[C O D E B A]
18Shrink left from 'C'610{"A":1,"B":1,"C":0}2Window 'ECODEBA'[O D E B A]
19Expand right to 'N'611{"A":1,"B":1,"C":0}2Window 'ECODEBA'[O D E B A N]
20Expand right to 'C'612{"A":1,"B":1,"C":1}3Window 'ECODEBANC'[O D E B A N C]
21Shrink left from 'O'712{"A":1,"B":1,"C":1}3Window 'CODEBANC'[D E B A N C]
22Shrink left from 'D'812{"A":1,"B":1,"C":1}3Window 'ODEBANC'[E B A N C]
23Shrink left from 'E'912{"A":1,"B":0,"C":1}2Window 'ODEBANC'[B A N C]
24End: right reached end, no smaller valid window912{"A":1,"B":0,"C":1}2Window 'BANC'[B A N C]
💡 Right pointer reached end of string; minimum window found is 'BANC'
Variable Tracker
VariableStartAfter Step 7After Step 13After Step 20Final
left00169
right05101212
window_counts{}{"A":1,"B":1,"C":1}{"A":1,"B":2,"C":1}{"A":1,"B":1,"C":1}{"A":1,"B":0,"C":1}
valid_count03332
min_windownull"ADOBEC""ADOBEC""ECODEBANC""BANC"
Key Moments - 3 Insights
Why do we shrink the window from the left only when the window is valid?
Because shrinking the window when it is not valid would lose required characters. See steps 7 to 8 where shrinking starts only after valid_count reaches 3.
Why do we keep track of valid_count instead of just checking window_counts every time?
valid_count tracks how many required characters meet their needed frequency, making it efficient to know when the window is valid without checking all counts each time. See column 'Valid Count' in execution_table.
Why does the window_counts for 'B' increase beyond the needed count?
Because the window can have extra occurrences of characters. We only care if counts meet or exceed required counts. See step 13 where 'B' count is 2 but valid_count remains 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 7, what is the minimum window substring found?
A"ADOBEC"
B"DOBEC"
C"BANC"
D"CODEBA"
💡 Hint
Check the 'Minimum Window' column at step 7 in the execution_table.
At which step does the valid_count first reach 3?
AStep 5
BStep 7
CStep 13
DStep 20
💡 Hint
Look at the 'Valid Count' column in execution_table to find when it first becomes 3.
If we did not shrink the window from the left, what would happen to the minimum window substring?
AIt would remain the same
BIt would become larger or not minimal
CIt would become empty
DIt would contain fewer characters
💡 Hint
Refer to steps 7 to 17 where shrinking left reduces window size and updates minimum window.
Concept Snapshot
Minimum Window Substring:
- Use two pointers (left, right) to slide over string
- Expand right to include chars until window valid
- Shrink left to minimize window while valid
- Track counts of chars and valid matches
- Return smallest valid window or empty if none
Full Transcript
Minimum Window Substring finds the smallest substring in a string that contains all characters of another string. We use two pointers, left and right, to create a sliding window. We expand the right pointer to include characters and update counts. When the window contains all required characters, we try to shrink it from the left to find the smallest valid window. We track counts of characters and how many required characters are fully matched. The process continues until the right pointer reaches the end. The smallest valid window found is returned, or empty if none exists.