HardHash TableStringSliding Window

1 approach, code in every language

Problem description

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.

After the pattern

Now solve one you haven't seen.

Shortest Kit Stretch is our own problem on the technique behind Minimum Window Substring: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free