Minimum Window Substring

Hard · Sliding Window · String · Hash Table · Sliding Window

Problem

Given strings s and t, return the shortest substring of s that contains every character of t, including repeats. If no such substring exists, return "".

The characters may appear in any order, and the window may contain extras.

Examples

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

The shortest window holding one A, one B and one C.

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

Only one a is available but two are required.

Constraints

Approaches

Follow-up

Can you find an algorithm that runs in O(m + n) time?

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…