A pantry log lists, in order, the ingredient letter used at each step of a cooking session, as a string s of uppercase letters. A recipe requires a multiset of ingredient letters, given as a string r (e.g. r = "AAB" requires at least two As and one B). Find the length of the shortest contiguous window of s that contains, for every letter appearing in r, at least as many occurrences as r requires. If no such window exists, print -1.
Line 1: a non-empty string s (the pantry log), uppercase letters only.
Line 2: a non-empty string r (the recipe requirement), uppercase letters only.
A single integer: the length of the shortest window of s covering the multiset of r, or -1 if no such window exists.
A-Z.Example 1
Input
ABAAB AA
Expected
2
Explanation
The recipe needs at least two A's. The window "AA" at positions 3-4 has length 2, which is the shortest possible since two letters are required.
Example 2
Input
BCBCBC A
Expected
-1
Explanation
The letter A never appears in the log, so no window can cover it: the answer is -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →