The “Permutation in String” problem asks whether one string, let's call it s2
, contains a permutation of another string s1
as a substring. In other words, we want to know if any substring of s2
has exactly the same characters as s1
, just possibly in a different order. This is essentially a sliding window pattern problem with a frequency comparison of character counts.
For example, if s1 = "ab"
and s2 = "eidbaooo"
, then s2
contains "ba", which is a valid permutation of s1
, so the output should be true
. If s2 = "eidboaoo"
, then there is no such substring that matches a permutation of s1
, and the result should be false
.
The optimal solution uses a fixed-size sliding window of length equal to the length of s1
. As the window slides over s2
, we compare the character frequency counts within the window to the frequency counts of s1
. If the counts match exactly, it means a valid permutation has been found.
To do this efficiently, we use two arrays of length 26 (one for each lowercase English letter). The first array stores the frequency of each character in s1
. The second array stores the frequencies of characters in the current window of s2
. We begin by populating both arrays with the first n1
characters—where n1
is the length of s1
.
If the frequency arrays match at the start, we can immediately return true
. If not, we begin sliding the window forward one character at a time. For each new character that enters the window, we increment its count in the s2
array. For each character that exits the window (from the left side), we decrement its count. After each shift, we compare the two arrays again. If they match, a valid permutation has been found.
The time complexity of this algorithm is O(n2), where n2 is the length of s2
. This is because we scan through s2
using a window of fixed size, and at each step we perform a comparison of two fixed-length arrays, which takes constant time. The space complexity is O(1) because both frequency arrays are of fixed size (26), regardless of input size.
If the length of s1
is greater than s2
, we can return false
immediately since a longer string cannot be a substring of a shorter one. If both strings are empty, or s1
is empty, the problem constraints typically assume this should return true
because an empty string is trivially a permutation of any substring.
The “Permutation in String” problem is a clever application of the sliding window technique combined with frequency counting. It highlights how fixed-size windowed comparisons can lead to extremely efficient algorithms, especially when the solution space is constrained to a known and limited alphabet such as lowercase letters. This pattern reappears in many string and substring detection problems, making it a fundamental approach for any programmer working with text data.