Menu
CoddyTech

Minimum Window Substring

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

If several substrings are shortest, return the leftmost. If none 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
The first window from the left that holds an n, an a and a p is appin, five characters. plan holds all three in four, and no run of three does.
challenge icon

Follow-up

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

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

Test #1

Test #2

Test #3

Arguments

s = "mappingtheplan"
t = "nap"

Expected Output

"plan"