#76

Minimum Window Substring

HardSliding WindowHas vizHash TableStringSliding Window
Related studio

Problem

Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string "".

The testcases will be generated such that the answer is unique.

Example 1

Input: s = "ADOBECODEBANC", t = "ABC"

Output: "BANC"

The minimum window containing A, B, and C is BANC.

Example 2

Input: s = "a", t = "a"

Output: "a"

The whole string is the window.

Example 3

Input: s = "a", t = "aa"

Output: ""

Not enough a's in s.

Constraints

  • m == s.length
  • n == t.length
  • 1 <= m, n <= 10^5
  • s and t consist of uppercase and lowercase English letters.
Loading visualization…

Explanation

Minimum Window Substring is a Hard interview problem tagged [Hash Table, String, Sliding Window]. The winning approach is typically **Sliding Window**.

Intuition

Each index enters and leaves the window at most once → amortized O(n).

Approach

Grow a right edge to include new elements; shrink the left edge when the window violates a constraint. Track the best window.

  1. Decide fixed vs variable window.
  2. Maintain a counter/map of window state.
  3. Expand right.
  4. Shrink left until valid.
  5. Update answer while valid.

Solution

Pattern-level skeleton only — not this problem’s exact answer. Adapt it to the statement.

Pattern templateSliding Window

Edge cases

  • •Empty or single-element input
  • •Duplicates and ties
  • •Maximum constraints / overflow
  • •Disconnected or unreachable states (for graphs)

Complexity

Time: O(n log n) – O(n²)

Space: O(n)

Before coding, restate the problem, name the pattern (Sliding Window), and write the invariant or DP state in one sentence.

Learn the topic

Got it?

Mark #76 Minimum Window Substring as understood when the pattern clicks.