ExamAdda Logo

Minimum Window Substring

Easy

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 an empty string.

If there is such a substring, it is guaranteed to be unique.

Example 1

Input

ADOBECODEBANC
ABC

Output

BANC

Explanation

The substring "BANC" is the smallest window that contains all the characters 'A', 'B', and 'C'.

Example 2

Input

a
a

Output

a

Explanation

The entire string itself is the minimum window containing all characters of t.

Constraints

m == s.length
n == t.length
1 ≤ m
n ≤ 10⁵
s and t consist of uppercase and lowercase English letters.

Hints:

Hint 1

Count the frequency of each character required from t.

Hint 2

Use a sliding window. Expand the right pointer until all required characters are present, then shrink the left pointer while maintaining validity to obtain the smallest window.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 5, 2026

Last updated

Aug 6, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output