You're given two strings, s and t. Return the shortest contiguous slice of s that contains every character of t, counting multiplicities — if t is "AABC", the slice must hold at least two As, one B, and one C. Order inside the slice doesn't matter; only the characters and their counts do. Return '' if no such slice exists. This is LeetCode 76, "Minimum Window Substring", and the canonical example is minWindow("ADOBECODEBANC", "ABC") → "BANC".
The slice must be a real substring of s (a slice()-style range), not a re-arrangement of characters from s. If multiple smallest windows exist, return the one whose start index is smallest.
function stringShortestSubstringContainingCharacters(
s: string,
t: string,
): string;
// LeetCode classic — "BANC" is the smallest slice covering A, B, and C.
stringShortestSubstringContainingCharacters('ADOBECODEBANC', 'ABC'); // 'BANC'
// No window — 's' contains no 'z'.
stringShortestSubstringContainingCharacters('hello world', 'z'); // ''
// Exact match — the only window that covers t is s itself.
stringShortestSubstringContainingCharacters('abc', 'abc'); // 'abc'
// t longer than s — impossible.
stringShortestSubstringContainingCharacters('abc', 'abcd'); // ''
// Repeated characters in t — the window must hold TWO A's and one C.
stringShortestSubstringContainingCharacters('AABBC', 'AAC'); // 'AABBC'
t has two 'A's, the answer must hold at least two 'A's. A window with one 'A' does not satisfy 'AA'.t may appear in any order inside the window, with anything between them.'A' and 'a' are different characters. Do not lowercase the inputs.t is empty or longer than s, no window is possible — return ''. The same applies if s is empty.s.s, not a reconstruction.Unlock the solution & editor
Runnable editor + tests
Solve in the browser with instant Jest feedback.
Detailed solutions
Walkthroughs, edge cases, and complexity notes.
Multi-framework variants
React, Vue, Vanilla, Angular — same question, different stacks.