Skip to content

Minimum Window Substring

HardSliding Window
Decode it first. Clues, constraints, pattern and plan before any code.

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.

s = "ADOBECODEBANC", t = "ABC"   →  "BANC"
s = "a",             t = "a"     →  "a"
s = "a",             t = "aa"    →  ""     (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).