Menu
CoddyTech

Minimum Window Substring

You get two strings, s and t. Find the shortest substring of s, a run of consecutive characters, that contains every character of t, counting repeats: if t holds a letter twice, the substring must hold it at least twice. The order does not matter, and the substring may hold other characters too.

If several substrings share the shortest length, return the leftmost one. If no substring of s contains all of t, return an empty string.

Function

minWindow(s: string, t: string) → string
sstring
the string to search in
tstring
the characters the window must contain, with repeats
Returnsstring
the shortest, then leftmost, substring of s that contains all of t, or an empty string

Constraints

  • 1 ≤ s.length ≤ 5 × 104
  • 1 ≤ t.length ≤ 104
  • s and t hold only English letters. Uppercase and lowercase letters are different characters.
  • When several substrings are shortest, the answer is the leftmost one; when none exists, it is "".

Examples

Input
s = "mappingtheplan"t = "nap"
Output
"plan"
Explanation
Reading from the left, the first window that holds an n, an a and a p is appin, five characters long. plan at the end holds all three in four characters, and no run of three characters does.

lock icon+17 hidden tests on Submit

challenge icon

Follow-up

When t uses only a few letters and s is long, most of s can never matter. Can you make the window jump only between the positions that hold a letter of t?

Reset code
def minWindow(s, t):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

s = "mappingtheplan"
t = "nap"

Expected

"plan"