FastPrepMinimum Window Substring

Minimum Window Substring

Character.AI logoCharacter.AI● HardNEW GRADPHONE SCREEN
Learn

Problem statement

Given strings s and t, find the shortest contiguous part of s that contains every character of t with its required multiplicity. Return that part of s. If no part qualifies, return an empty string.

For these test cases, a shortest qualifying window, when one exists, is unique. Treat uppercase and lowercase letters as different characters.

Function

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

Examples

Example 1

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

The final four characters contain each required letter; no shorter window does.

Example 2

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

The only character supplies the target.

Example 3

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

There is only one copy of a, so no valid window exists.

Constraints

  • 1 ≤ s.length, t.length ≤ 10^5.
  • Both strings contain only uppercase and lowercase English letters.
  • The total input fits in memory.

More Character.AI problems

See Character.AI hiring insights
public String minWindow(String s, String t) {
    // Write your code here.
}
s"ADOBECODEBANC"
t"ABC"
expected"BANC"
Checking account…