AceYourInterviews
Checking sign-in…

25. Minimum Window Substring

AdvancedMultiplicity deficits with grow-then-shrink coverage

Choose the shortest continuous part of the source that contains every character required by the target. Extra characters are allowed inside the chosen window, but required repeated characters must all be present.

Order within the target does not impose an order on the window. Follow the contract for ties and for the case where no covering window exists.

Example 1

Input

source="ADOBECODEBANC", target="ABC"

Output

"BANC"

Explanation: BANC covers A,B,C in length four; target order is not required.

Example 2

Input

source="AAABBC", target="AABC"

Output

"AABBC"

Explanation: Two A occurrences are necessary, so ABC alone would not cover the target.

Constraints

  • 0 <= source.length, target.length <= 105
  • 0 <= character code <= 127
  • Matching is case-sensitive ASCII. Return the earliest start when minimum lengths tie.
Hints
Hint 1

Which target occurrences are still missing from the current window?

Hint 2

How do negative deficits represent surplus copies?

Hint 3

Grow until missing is zero; record and shrink while coverage survives.

Practice on LeetCode ↗
Code
Saved on this deviceLn 1, Col 1

Cover, shrink, and improve the best boundaries

"ADOBECODEBANC"
"ABC"
"BANC"