Shortest Sub{string,array}
while expand + while shrink
Condition for shrink usually will just be the complement/negation/opposite of expansion.
Naive
209. Minimum Size Subarray Sum
Find the minimum length of a contiguous subarray whose sum is greater than or equal to target.
Expansion:
- Expand the window by incrementing
right, adding tototaluntil it reaches or surpasses the target.
Shrinking:
- Once the target is met, shrink from the left to potentially reduce window size while still meeting the sum requirement. The logic is integrated within this phase.
Logic:
- Within the shrinking phase, before decrementing
total, updateminito reflect the shortest subarray found.
- Time
- O(n)
rightandlefteach advance acrossnums(lengthn) at most once, so every element is added to and removed from the window at most once.- Space
- O(1)
- Only
left,right,mini, andtotalare tracked.
2260. Minimum Consecutive Cards to Pick Up
Find the minimum number of consecutive cards needed to pick up at least one pair of matching cards. The window expands until a duplicate is found, then shrinks to find the minimum window containing the pair.
- Time
- O(N)
- Each card is considered at most once for inclusion and removal.
- Space
- O(N)
- The counter could contain an entry for every unique card.
Two Strings
76. Minimum Window Substring
Find the smallest window in string s that contains all characters of string t.
- Cache all frequencies required by
tinrequired_counter. - Track frequencies of characters in the window using
counter, and maintainrequiredas the count of unique characters still needed.
Expansion:
- Expand until all required characters are included (
required == 0). Decrementrequiredwhen a character's frequency matches its required count.
Shrinking:
- Shrink while the requirement still holds. Increment
requiredwhen a necessary character is removed.
Logic:
- During shrinking, capture the smallest valid window.
- Time
- O(m + n)
m = len(t),n = len(s). Buildingcounter = Counter(t)scans every character oftonce -O(m)- then theright/lefttwo-pointer scan visits every character ofsat most once each -O(n).- Space
- O(1)
counter(the single dict-backedCounter, not two) tracks per-character counts for whatever distinct characters appear insandt; bounded by the fixed English-letter alphabet, so it never grows withmorn.
Smallest window containing 0, 1 and 2
A special case of "Minimum Window Substring" where t = "012". Find the shortest substring that includes all digits "0", "1", and "2" at least once. Uses the canonical custom Counter initialized with "012"; an anagram-style window is valid when counter.distinct_count() == 0.
- Time
- O(N)
- Linear pass with early return optimization.
- Space
- O(1)
- Counters limited by the fixed set of characters ("0", "1", "2").
Bit Frequency Window
3095. Shortest Subarray With OR at Least K I
Same expand/shrink skeleton as the sum-based shortest-window problems, but OR isn't invertible by subtraction like a sum is - there's no way to "undo" an OR when an element leaves the window. The fix: track how many elements currently in the window have each of the 32 bits set, in bit_freq. A bit is "on" in the window's OR whenever its count is >= 1, and a bit only turns back off once its count drops to 0 - so add_freq/rem_freq can maintain the window's OR incrementally in both directions by bumping counts up on expansion and down on shrink.
- Time
- O(N)
- Each element enters and leaves the window at most once;
add_freq/rem_freq/atLeastKeach do fixed 32-bit work, so total work is O(32*N) = O(N). - Space
- O(1)
bit_freqis a fixed-size array of 32 counters.