JavaScriptHardSliding Window35 min

Find the minimum covering substring

Return the shortest substring of source containing all characters of target with their required multiplicities. Break ties by earliest start; return an empty string if target is empty or no window exists. All input is valid JSON satisfying the constraints; do not mutate the meaning of values through type coercion. Interviewers use this problem to assess expand a window until it covers the target then shrink it greedily.

Input

A JSON array [source, target] of ASCII strings.

Output

Print one compact JSON value: a string.

Constraints

Each string has length <= 10000.

Example

Input

["ADOBECODEBANC","ABC"]

Output

"BANC"

Example 2 input

["abxxab","ab"]

Output

"ab"

Boundary case: Update the best answer only for a strictly shorter window

Public test cases (2)

  • Input

    ["ADOBECODEBANC","ABC"]

    Expected output

    "BANC"
  • Input

    ["aa","aa"]

    Expected output

    "aa"
Hints (2)
  1. Track missing character occurrences rather than just distinct characters
  2. Update the best answer only for a strictly shorter window

Target complexity: time O(n+m) · space O(1) auxiliary for fixed ASCII alphabet, excluding returned substring

Your solution

Checking runner availability…

Reference solution

Sign in for solution
All JavaScript programs