Problem 335265 · hard · Level 03 Linear Management & Searching

Smallest Cover of the Rune Set

sliding window · variable-size window · character counts

A scroll is a string scroll, and a spell is a string spell (both may contain uppercase and lowercase letters). Return the shortest substring of scroll that contains every character of spell, including repeats (if spell has two 'a's, the substring needs at least two 'a's). If several shortest substrings exist, return the one that starts first. If no such substring exists, return the empty string "".

Examples

Input:  scroll = "ADOBECODEBANC", spell = "ABC"
Output: "BANC"

Input:  scroll = "a", spell = "a"
Output: "a"

Input:  scroll = "a", spell = "aa"
Output: ""

Constraints

  • 0 <= len(scroll) <= 10**5, 1 <= len(spell) <= 10**5
  • Both strings consist of English letters.
  • Target complexity: O(n + m) time; checking every substring is far too slow for the largest tests.

Goals

  • Track how many required characters are fully satisfied, with multiplicity
  • Shrink to the minimal valid window before recording it
Starting Python…