2Brute2Best
🔥0

Minimum Window Substring

Hard·LeetCode #76 Pro OnlyInterview-Level
LeetCode
01

Problem Statement

You are given two strings s and t.

Find the shortest substring of s that contains every character of t, including repeats. For example, if t = "AAB", the substring needs at least two 'A' and one 'B'. Other characters may also appear in it.

Return that substring. If no such substring exists, return the empty string "".

The test data guarantees the answer is unique.

Input

Two strings s and t of uppercase and lowercase English letters.

Output

The shortest substring of s that covers all characters of t (with counts), or "" if none exists.

Example 1
Input: s = "ADOBECODEBANC", t = "ABC"
Output: "BANC"

"BANC" has an A, a B and a C. No shorter substring has all three.

Example 2
Input: s = "a", t = "a"
Output: "a"

The whole string is the answer.

Example 3
Input: s = "a", t = "aa"
Output: ""

t needs two 'a' but s has only one, so no substring works.

Constraints
  • • 1 ≤ s.length, t.length ≤ 10^5
  • • s and t contain only uppercase and lowercase English letters
  • • Follow-up: can you do it in O(|s| + |t|) time?
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“minimum window / shortest substring”
→ Contiguous range, minimise length → variable window, record inside the shrink loop.
“contains all characters of t”
→ The rule is about counts inside the window compared with a target → need map.
“including duplicates”
→ Counts, not a set. "AAB" needs two A's.
03

Approach Complexity Ladder

Brute Force — every start, grow until it covers tO(|s|² · 128)O(128)

For each start, grow right and check all 128 counts after every step

Better — sliding window with a full count checkO(128 · |s| + |t|)O(128)

Grow right; while the window covers t (checked over 128 chars), record and shrink

Optimal — sliding window with a `missing` counterO(|s| + |t|)O(128)

Track how many characters of t are still missing; covered when missing == 0

Brute Force Intuition

Try every start. Grow the end one character at a time and keep counts for the window. After each step, compare the window's counts with t's counts for all 128 characters. The first end where the window covers t gives the shortest window for that start; stop there and move to the next start.

Recognition rule for this sub-pattern:

Validity depends on the counts of things inside the window (distinct types, most frequent char, required letters). Keep a count map that updates on every add/remove.
Remember this · Sliding Window
Trigger
Contiguous subarray / substring + longest, shortest, maximum, count, or "of size K".
Think
Can I keep a valid range and move it, instead of checking every subarray from scratch?
Key question
When is my current window valid?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.