Minimum Window Substring

Hard#76variable size

You get two strings, s and t. Find the shortest piece of s, letters in a row, that contains every letter of t, as many times as t has it. If there is none, the answer is an empty string. Our example: s = "baxcab", t = "abc" — the answer is "cab".

input
s and t: strings of letters; a and A are different characters.
returns
The shortest substring of s that has every character of t, each at least as many times as in t; "" if there is none.
constraints
  • 1 ≤ s.length ≤ 10000
  • 1 ≤ t.length ≤ 10000
  • s, t: uppercase and lowercase English letters
  • the shortest window is one string, or there is none
our 12 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {string} s
 * @param {string} t
 * @return {string}
 */
function minWindow(s, t) {

}
Ctrl ↵
format
reset

saved in this browser

12 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more sliding window problems

Pattern “Sliding window” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.