Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise.
In other words, return true if one of s1's permutations is the substring of s2.
Input: s1 = "ab", s2 = "eidbaooo"
Output: true
Explanation: s2 contains one permutation of s1 ("ba").
Input: s1 = "ab", s2 = "eidboaoo"
Output: false
s1 and s2 consist of lowercase English letters.The brute-force approach involves checking all permutations of s1 and seeing if any of them is a substring of s2. Although not efficient, it helps build understanding of the problem.
s1.s2.n is the length of s1 and m is the length of s2.Since a permutation of s1 should have the same characters with the same frequency, for each substring of s2 with length s1, we check if its sorted version matches the sorted s1.
s1.s2 with the length of s1, sort it and compare with sorted s1.m is the size of s2 and n is the size of s1.s1 and each substring.The optimized approach uses a sliding window technique with two arrays (or maps) to keep track of character counts. If at any point the character counts between the current window in s2 and s1 match, we found a permutation.
s1 and the current window.s2 with a window of the size of s1, updating the counts.m is the length of s2.