</>

567. Permutation in String

Medium NeetCode 150
Sliding Window Time O(n) Space O(1)
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
Sliding Window
Time
O(n)
Space
O(1)

Standard Solution (C++)

SummaryGiven two strings s1 and s2, determine whether any contiguous substring of s2 is an anagram (permutation) of s1 — return true if one exists, false otherwise.

Approach
  • Maintain a fixed-size window of length s1.size() sliding over s2, tracking character frequency differences in a 26-element array.
  • A counter formed starts at the number of distinct characters in s1; it decrements each time a character's count in the window exactly satisfies s1's requirement, and increments when it drops below.
  • When formed == 0, every character of s1 is fully matched inside the current window, so a permutation was found.
  • Shrinking the left edge mirrors the right-edge expansion, keeping window size fixed at |s1|.
#include <string>
#include <vector>
using namespace std;

class Solution {
public:
    bool checkInclusion(string s1, string s2) {
        if (s1.size() > s2.size()) return false;

        // need[c] = how many more of character c we still need in the window
        // positive  => still needed, negative => window has excess
        vector<int> need(26, 0);
        for (char c : s1) ++need[c - 'a'];

        // formed = number of distinct characters still unsatisfied
        int formed = 0;
        for (int x : need) if (x > 0) ++formed;

        int left = 0;
        for (int right = 0; right < (int)s2.size(); ++right) {
            // Expand right edge
            int rc = s2[right] - 'a';
            --need[rc];
            if (need[rc] == 0) --formed;  // just satisfied this character

            // Window reached target size: check, then shrink left edge
            if (right - left + 1 == (int)s1.size()) {
                if (formed == 0) return true;

                int lc = s2[left] - 'a';
                if (need[lc] == 0) ++formed;  // about to become unsatisfied
                ++need[lc];
                ++left;
            }
        }
        return false;
    }
};
Time O(n) where n = |s2|; each character is visited at most twice (right expansion + left shrink) Space O(1) — fixed 26-element frequency array, independent of input size

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

class Solution {
public:
    bool checkInclusion(string s1, string s2) {
        unordered_map<int, int> umap; // may improve by using vector<int> (26, 0);
        int len1=s1.length();
        int len2=s2.length();

        if(len1>len2){
            return false;
        }

        // use hashmap to record char freq in s1
        for(const char& c: s1){
            ++umap[c];
        }

        // use matchCount to record if all the chars in this window are matched
        int matchCount=0;

        // keep a window to check
        int left=0;
        for(int right=0; right<len2; ++right){
            if(umap[s2[right]]>0){
                ++matchCount;
            }
            --umap[s2[right]];

            if(matchCount==len1){
                return true;
            }

            if(right-left+1==len1){
                ++umap[s2[left]];
                if(umap[s2[left]]>0){
                    --matchCount;
                }
                ++left;
            }
        }

        return false;

    }
};

// Time Complexity: O(n)
// Space Complexity: O(1), the number of entries in the hash map is at most 26.
Time O(n) Space O(1)

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern