#K4916. Minimum Window Substring Length

    ID: 28581 Type: Default 1000ms 256MiB

Minimum Window Substring Length

Minimum Window Substring Length

Given two strings S and T, your task is to find the length of the shortest contiguous substring of S that contains all the characters in T (including multiplicities). If no such substring exists, output -1.

The problem can be formally stated as follows: Find the minimum length of a substring S[i...j] such that for every character c in T, \[ \mathrm{count}_{S[i..j]}(c) \geq \mathrm{count}_T(c), \] otherwise return -1.

Note: The substring must be contiguous and the characters may appear in any order.

inputFormat

The input is read from stdin and consists of two lines:

  • The first line contains the string S.
  • The second line contains the string T.

outputFormat

Output the length of the shortest substring of S that contains all characters of T. If no such substring exists, output -1.

The output should be written to stdout.

## sample
adobecodebanc
abc
4