Check If a String Contains a Permutation of Another
Implement containsPermutation
Given two lowercase strings
s1 and s2, return true if s2 contains a contiguous substring that is a permutation (an anagram, rearranged) of s1.
A permutation doesn't care about order — only about having exactly the same letters, with exactly the same counts. That turns this from a string-matching problem into a counting problem: a window of s2 is a match precisely when its 26-letter frequency count equals s1's. Rebuilding that count for every window works, but the sliding windowSliding WindowMaintaining a running result over a contiguous range that grows or shrinks one element at a time, instead of recomputing the result for every range from scratch. technique keeps the count updated in O(1) per slide — bump the letter entering, drop the letter leaving — instead of recounting all of s1.length letters every time.
Example 1:
Input: s1 = "ab", s2 = "eidbaooo"
Output: true
Example 2:
Input: s1 = "ab", s2 = "eidboaoo"
Output: false
Example 3:
Input: s1 = "adc", s2 = "dcda"
Output: true
+ 9 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ s1.length ≤ s2.length ≤ 10⁴ - ●
s1 and s2 consist of lowercase English letters only
s1 =
ab
s2 =
eidbaooo