InferenceAI

Enter at least two characters to search.

Start free

Smallest Covering Window

hardSliding Window40 min50 XP

Return the shortest substring of s that contains every character of t, including repeats. If several are equally short, return the leftmost. Return "" if none exists.

Requirements

  • Run in O(∣s∣+∣t∣)O(|s| + |t|) time.

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT["ADOBECODEBANC","ABC"]
TASKsmallest_covering_window()
EXPECTED OUTPUT"BANC"

Start with the input values shown in this example.

Example 1 of 3

Input: s = "ADOBECODEBANC", t = "ABC"

Output: "BANC"

Explanation: classic

Input: s = "a", t = "a"

Output: "a"

Explanation: exact match

Input: s = "a", t = "aa"

Output: ""

Explanation: not enough copies

Hint 1

Expand the right edge until the window covers t, then shrink the left edge while it still covers.

Hint 2

Track how many required characters are still missing.

Topics

DSASliding WindowPython