Permutation in String

mediumSliding Windowfixed-size window + frequency count compareSolve on LeetCode#567

Part of Practice: Counted Windows & Deques

Given two lowercase strings s1 and s2, return true if s2 contains any permutation of s1 as a substring, and false otherwise. A permutation is a rearrangement — "ba" is a permutation of "ab" — so you are really asking: does some window of s2, exactly |s1| characters wide, use precisely the same letters with the same counts as s1?

Both strings can be up to 10^4 characters. If s1 is longer than s2, the answer is immediately false.

Examples

Input:
s1 = "ab", s2 = "eidbaooo"
Output:
true

The window "ba" (positions 3-4 of s2) is a permutation of "ab".

Input:
s1 = "ab", s2 = "eidboaoo"
Output:
false

s2 contains an a and a b, but never adjacent to each other, so no 2-wide window matches.

Hints

One at a time — struggle first, peek second.

Your thinking space — verify on LeetCode, then mark it.