Algorithm-Day12-Minimum-Window-Substring-lc-76
š§© Problem Description
Given two strings s
and t
, return the minimum window substring of s
such that every character in t
(including duplicates) is included in the window.
If there is no such substring, return the empty string ""
.
Example:
1 | Input: s = "ADOBECODEBANC", t = "ABC" |
š” Brute Force? Not Efficient
Try all substrings of s
and check if they contain all characters of t
.
- Time: O(n³)
- ā Way too slow
š” Optimal Approach: Sliding Window + Hash Map
š§ Key Ideas
- Use a sliding window defined by two pointers:
left
andright
- Expand the window to the right until all required characters are included
- Then try to shrink the window from the left to find the minimum
- Track character counts using hash maps
š¢ Java Code
1 |
|
ā± Time and Space Complexity
- Time Complexity: O(n + m)
Wheren
is length ofs
,m
is length oft
- Space Complexity: O(m)
For the hash maps tracking counts
āļø Summary
- A classic and foundational sliding window problem
- Mastering this helps with problems like:
lc-567 (Permutation in String)
lc-438 (Find All Anagrams)
lc-239 (Sliding Window Maximum)
Key challenge: tracking when the window becomes valid and shrinking it optimally.