0
0
DSA Pythonprogramming~10 mins

Minimum Window Substring in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Minimum Window Substring
Initialize frequency map of target chars
Start sliding window
Expand right pointer to include chars
Check if window contains all target chars
Expand right
Update minimum window
Move left pointer
Repeat until right reaches end
Return min window
We use two pointers to create a sliding window that expands and shrinks to find the smallest substring containing all target characters.
Execution Sample
DSA Python
def min_window(s, t):
    from collections import Counter
    need = Counter(t)
    missing = len(t)
    left = start = end = 0
    for right, char in enumerate(s):
        if need[char] > 0:
            missing -= 1
This code initializes the frequency map of target chars and starts expanding the right pointer to find a valid window.
Execution Table
StepOperationWindow [left:right]Chars NeededActionMinimum Window Found
1Initialize[]{A:1, B:1, C:1}Set need map and missing countNone
2Expand right to 'A'[A]{A:0, B:1, C:1}Found needed 'A', missing=2None
3Expand right to 'D'[A,D]{A:0, B:1, C:1}Char 'D' not neededNone
4Expand right to 'O'[A,D,O]{A:0, B:1, C:1}Char 'O' not neededNone
5Expand right to 'B'[A,D,O,B]{A:0, B:0, C:1}Found needed 'B', missing=1None
6Expand right to 'E'[A,D,O,B,E]{A:0, B:0, C:1}Char 'E' not neededNone
7Expand right to 'C'[A,D,O,B,E,C]{A:0, B:0, C:0}Found needed 'C', missing=0Window [0:6] = 'ADOBEC'
8Shrink left from 'A'[D,O,B,E,C]{A:1, B:0, C:0}Lost 'A', missing=1Window remains 'ADOBEC'
9Expand right to 'O'[D,O,B,E,C,O]{A:1, B:0, C:0}Char 'O' not neededWindow remains 'ADOBEC'
10Expand right to 'D'[D,O,B,E,C,O,D]{A:1, B:0, C:0}Char 'D' not neededWindow remains 'ADOBEC'
11Expand right to 'E'[D,O,B,E,C,O,D,E]{A:1, B:0, C:0}Char 'E' not neededWindow remains 'ADOBEC'
12Expand right to 'B'[D,O,B,E,C,O,D,E,B]{A:1, B:-1, C:0}Extra 'B', missing=1Window remains 'ADOBEC'
13Expand right to 'A'[D,O,B,E,C,O,D,E,B,A]{A:0, B:-1, C:0}Found needed 'A', missing=0Window [3:11] = 'EBANC'
14Shrink left from 'D'[O,B,E,C,O,D,E,B,A]{A:0, B:-1, C:0}Char 'D' not neededWindow [3:11] = 'EBANC'
15Shrink left from 'O'[B,E,C,O,D,E,B,A]{A:0, B:-1, C:0}Char 'O' not neededWindow [3:11] = 'EBANC'
16Shrink left from 'B'[E,C,O,D,E,B,A]{A:0, B:0, C:0}Lost extra 'B', missing=0Window [6:11] = 'BANC'
17Shrink left from 'E'[C,O,D,E,B,A]{A:0, B:0, C:-1}Lost 'C', missing=1Window remains 'BANC'
18End of string reached[C,O,D,E,B,A]{A:0, B:0, C:-1}StopMinimum window is 'BANC'
💡 Right pointer reached end of string; minimum window found.
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 7After Step 8After Step 13After Step 16Final
left00001366
right-10355101010
missing32101000
need[A]10001000
need[B]11000000
need[C]111000-1-1
min_windowNoneNoneNone'ADOBEC''ADOBEC''EBANC''BANC''BANC'
Key Moments - 3 Insights
Why do we decrease 'missing' only when need[char] > 0?
Because 'missing' counts only needed characters. If need[char] <= 0, it means we have extra occurrences, so 'missing' should not decrease. See steps 12 and 16 in execution_table.
Why do we shrink the window from the left only when missing == 0?
Shrinking is done only when the window contains all needed chars (missing == 0) to try to find a smaller valid window. See steps 7 to 8 and 13 to 16.
What happens when we lose a needed character while shrinking?
When shrinking removes a needed char, missing increases, making the window invalid. We then stop shrinking and continue expanding. See step 8 where missing goes from 0 to 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 7, what is the minimum window found?
A'ADOBEC'
B'BANC'
C'EBANC'
DNone
💡 Hint
Check the 'Minimum Window Found' column at step 7 in execution_table.
At which step does the 'missing' variable first become zero?
AStep 13
BStep 5
CStep 7
DStep 16
💡 Hint
Look at the 'missing' column in variable_tracker and execution_table rows.
If we remove the shrinking step, how would the minimum window change?
AIt would be the entire string
BIt would remain the first valid window found
CIt would become empty
DIt would be the last character only
💡 Hint
Shrinking tries to reduce window size; without it, minimum window stays as first valid window (see steps 7 and 8).
Concept Snapshot
Minimum Window Substring:
- Use two pointers (left, right) to create a sliding window.
- Expand right to include chars until all needed chars are in window.
- Shrink left to minimize window while keeping all chars.
- Track counts with a frequency map and missing count.
- Return smallest window containing all target chars.
Full Transcript
The Minimum Window Substring problem finds the smallest substring in a string that contains all characters of a target string. We use a sliding window approach with two pointers: left and right. We keep a frequency map of needed characters and a count of how many are missing. We expand the right pointer to include characters, decreasing missing when a needed char is found. When missing reaches zero, the window contains all needed chars. Then we try to shrink the window from the left to find a smaller valid window. If shrinking removes a needed char, missing increases and we stop shrinking. We repeat until the right pointer reaches the end. The smallest window found during this process is returned.