HardHash TableStringSliding Window

Minimum Window Substring

LeetCode
1 approach, code in all languages

You are given two strings s and t. Find the shortest contiguous substring of s that contains every character of t, counting duplicates. In other words, if a character appears multiple times in t, the chosen window must contain it at least that many times.

Return that minimum-length substring. If s has no window that covers all of t, return the empty string "".

The answer is guaranteed to be unique when it exists.

Example 1

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

Output: "BANC"

The substring "BANC" contains one A, one B, and one C, and no shorter window of s covers all three.

Example 2

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

Output: "a"

The entire string is the only window, and it already contains the required character.

Constraints

  • m == s.length
  • n == t.length
  • 1 <= m, n <= 10^5
  • s and t consist of uppercase and lowercase English letters.
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now