Problem · String

Minimum Window Substring

Learn this problem
HardLyft logoLyftFULLTIMEPHONE SCREEN

Problem statement

Given strings s and t, return the shortest contiguous substring of s that contains every character of t, including duplicate occurrences.

If no such substring exists, return the empty string. The answer is guaranteed to be unique whenever it exists.

Function

minWindow(s: String, t: String) → String

Examples

Example 1

s = "ADOBECODEBANC"t = "ABC"return = "BANC"

"BANC" contains A, B, and C, and no shorter substring contains all three.

Example 2

s = "a"t = "a"return = "a"

The entire source string is the only window and contains the required character.

Example 3

s = "a"t = "aa"return = ""

The source string has only one a, so it cannot satisfy the target multiplicity.

Constraints

  • 1 <= s.length, t.length <= 2 * 10^5.
  • s and t contain printable ASCII characters.
  • If a valid minimum window exists, it is unique.

More Lyft problems

drafts saved locally
public String minWindow(String s, String t) {
  // write your code here
}
s"ADOBECODEBANC"
t"ABC"
expected"BANC"
checking account