Skip to content
Decoder

Minimum Window Substring

Hard

Problem

Given strings s and t, return the shortest substring of `s` that contains every character of `t`, including repeats (if t has two as, the window needs at least two as). If no such substring exists, return "". If several shortest windows exist, return the one that starts first.

Example 1

Input
s = "ADOBECODEBANC", t = "ABC"
Output
"BANC"

Example 2

Input
s = "a", t = "a"
Output
"a"

Example 3

Input
s = "a", t = "aa"
Output
""
Why
only one a available

Constraints

  • 1 <= s.length, t.length <= 10^5
  • s and t consist of uppercase and lowercase English letters (case-sensitive).
  • Aim for O(s.length + t.length).

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.