You get two strings, s1 and s2. Return true if s2 contains a run of s1.length consecutive characters that is a rearrangement of s1: the same letters as s1, each one the same number of times, in any order. Otherwise return false.
A run that matches s1 exactly counts too, since keeping the original order is one of the rearrangements.
The three letters starting at index 2 are tar, which rearranges into rat.
The runs of three letters are bab, abc, bca and cab. Each holds at most one a, but s1 needs two.
s1 has four letters and s2 only three, so no run of s2 is long enough.
Constraints
1 <= s1.length <= 10⁴1 <= s2.length <= 10⁴s1 and s2 contain only lowercase English letters.A run of s2 is a rearrangement of s1 exactly when both hold the same count of every letter, so compare letter counts instead of trying orderings.
Forget orderings. Two strings are rearrangements of each other exactly when they hold the same count of every letter. tar and rat both have one t, one a and one r, and that is all that matters. So the question becomes: does some run of s1.length letters in s2 have the same 26 letter counts as s1?
Time O(n + 26)Space O(1)
The full walkthrough and code are part of Premium
true