Minimum Window Substring
1 approach, code in every language
Problem description
You are given two strings s and t. Find the shortest contiguous substring of s that contains every character of t, counting duplicates. In other words, if a character appears multiple times in t, the chosen window must contain it at least that many times.
Return that minimum-length substring. If s has no window that covers all of t, return the empty string "".
The answer is guaranteed to be unique when it exists.
Example 1
Input: s = "ADOBECODEBANC", t = "ABC"
Output: "BANC"
The substring "BANC" contains one A, one B, and one C, and no shorter window of s covers all three.
Example 2
Input: s = "a", t = "a"
Output: "a"
The entire string is the only window, and it already contains the required character.
Constraints
m == s.lengthn == t.length1 <= m, n <= 10^5s and t consist of uppercase and lowercase English letters.
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Shortest Kit Stretch is our own problem on the technique behind Minimum Window Substring: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free