Back to DSA

Minimum Window Substring

hard
Acceptance: 40%
StringsSliding Window

Two strings are provided: a source string and a required-characters string. Find the smallest contiguous window within the source that includes every character from the required-characters string, respecting duplicate counts. If no valid window exists, return an empty string. The answer is guaranteed to be unique when it exists.

Examples

Example 1:
Input:source = "XAYBEZCAD", required = "ABC"
Output:"BEZCA"
Explanation: The window from index 3 to 7 is 'BEZCA', which contains one each of A, B, and C. No shorter window covers all three.
Example 2:
Input:source = "x", required = "xx"
Output:""
Explanation: The source has only one 'x' but two are required, so no valid window exists.

Hints

00:00
1234567