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.lengthn == t.length1 <= m, n <= 10^5s and t consist of uppercase and lowercase English letters.See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
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