Prompt:

You are given two strings s1 and s2.

Return true if s2 contains a permutation of s1, or false otherwise. That means if a permutation of s1 exists as a substring of s2, then return true.

Both strings only contain lowercase letters.

Example 1:

Input: s1 = "abc", s2 = "lecabee"
 
Output: true

Explanation: The substring "cab" is a permutation of "abc" and is present in "lecabee".

Example 2:

Input: s1 = "abc", s2 = "lecaabee"
 
Output: false

Constraints:

  • 1 <= s1.length, s2.length <= 10000

Solution:

class Solution {
public:
    bool checkInclusion(string s1, string s2) 
    {   
        //inital check for string lengths
        if(s1.length() > s2.length()) {return false;}
 
        //set up the string one counter array and count it
        std::array<int, 26> s1Array= {};
        for(char ch : s1) { s1Array[ch-'a']++; }
 
        //make the initial window and the first check
        std::array<int, 26> s2Array= {};
        for(int i = 0; i < s1.length(); i++)
        {
            s2Array[s2[i]-'a']++;
        }
 
        //otherwise slide the window until the correct array is found or right reaches send of second string
        for(int right = s1.length(); right < s2.length(); right++)
        {
            //check to see if the array values are correct
            if(s2Array == s1Array) {return true;}
 
            //increment the right and add this to the array count
            s2Array[s2[right]-'a']++;
 
            //increment the left pointer and decrement the value in the array
            s2Array[s2[right-s1.length()]-'a']--;
        }
 
        return s2Array == s1Array;
 
    }   
};
 

Explanation:

This one was a pretty fun one because it felt a lot more like a traditional sliding window problem given that the window was of a fixed size this whole time. So basically the window is actually the size of s1 right, so you can throw out a few things right away like if the second string is less than the first one and what not.

Basically though, you do just have this fixed window size that you slide along the array and then compare the given string to the permutation of the original first string. The thing here that got me again however is I wanted to use a hashset but the better thing to have done is use one static version of an std::array because you can directly compare them in O(26) time, but you just index into it based on the character value and then increment the count for your main string

This becomes your root array that you sort of check everything against. Then for the second array you set the start of the window so you have the window set up and can start moving it, then what you do is whenever you move the window you do the classic sliding window thing where you increment the element your adding and decrement the element your leaving behind, since the most of the inner elements does not change, with this you save a lot of leg work. You basically then update the array over and over and each iteration you check to see if you have found the correct substring.

If you do then you return true of course, but then there is the edge case of what if the very final window is the one where the substring lives, so what you do is at the very end outside of the while loop there is that s2Array = s1Array which is the final check, if it is true then all good, if not then you know you have checked all other possible combinations so you return false.