Problem · Sliding Window

Minimum Covering Substring

Learn this problem
HardByteDance logoByteDanceFULLTIMEONSITE INTERVIEW

Problem statement

Given strings s and t, return the shortest contiguous substring of s that contains every character of t with at least its required multiplicity.

  • Character matching is case-sensitive.
  • If several covering substrings have minimum length, return the leftmost one.
  • Return the empty string when no covering substring exists.

Function

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

Examples

Example 1

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

BANC is the shortest substring containing A, B, and C.

Example 2

s = "aaabcbcba"t = "aabc"return = "aabc"

The substring from index 1 through 4 contains two a characters plus b and c; no shorter window can satisfy four required characters.

Constraints

  • 0 <= s.length, t.length <= 100000.
  • s and t contain case-sensitive ASCII characters.

More ByteDance problems

drafts saved locally
public String minCoveringSubstring(String s, String t) {
    // TODO: return the leftmost shortest covering substring.
}
s"ADOBECODEBANC"
t"ABC"
expected"BANC"
checking account