Smallest Window Containing All Characters of Pattern

MEDIUM · C · 2000ms

Antu is searching a long log string for the SMALLEST window that contains every character of a given complaint keyword pattern. Given string S and pattern P, find the length of the smallest substring of S that contains all characters of P (with at least the same multiplicity), or -1 if impossible.

Input

Two strings S and P.

Output

Minimum window length, or -1.

Constraints

1 ≤ |P| ≤ |S| ≤ 10^5

Sample Input

ADOBECODEBANC
ABC

Sample Output

4
main.c
Loading editor…

Write C, then Run (custom I/O) or Submit (sample tests). ⌘/Ctrl+Enter submits.

Smallest Window Containing All Characters of Pattern · DIU ContestHub