Minimum Window Substring

Asked byFacebookAmazonLinkedInLyftMicrosoftAirbnb

Problem

Given two strings s and t, return the shortest substring of s that contains every character of t, including duplicates, at least as many times as it appears in t. Return an empty string if no such substring exists.

Examples

Example 1
Input:s = "ADOBECODEBANC", t = "ABC"
Output:"BANC"
Example 2
Input:s = "a", t = "aa"
Output:""
There aren't two 'a's in s.

Constraints

  • 1 <= s.length, t.length <= 10^5

Solve it in the editor. Sign in free to run your Python or JavaScript against test cases, get a verdict, and track your attempts.

Solve on FeatCode →

How to approach it: the Sliding Window pattern

A window — a contiguous subarray or substring — expands and shrinks as it moves across the input, so you track a running condition instead of recomputing it from scratch for every possible window.

Look for this pattern when

  • The problem asks for the longest/shortest/best contiguous subarray or substring meeting a condition.
  • Brute force would recompute a sum or count for every possible window — a sign that work can be shared between overlapping ranges.

Read the full Sliding Window guide →

Video walkthroughs

Original problem on LeetCode ↗

More Sliding Window problems