mediumSliding WindowSliding Window

Permutation in String

mediumTime: O(n)Space: O(1)

Recognize the pattern

check if one string is a permutation of a substringthe same-size window keeps movingcharacter frequency match

Brute force idea

If you approach Permutation in String in the most literal way possible, you get this: Check every substring of s1's length in s2. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.

Better approach

The deeper shift in Permutation in String is this: Slide a window of size len(s1) over s2, maintain frequency counts. Once you hold onto the right piece of information from moment to moment, the problem feels less like trial and error and more like following a shape that was there all along.

Key invariant

At the center of Permutation in String is one steady idea: A permutation exists when the window's character counts exactly match s1's counts. When you keep that truth intact, each local choice supports the larger solution instead of fighting it.

Watch out for

A common way to get lost in Permutation in String is this: Comparing entire frequency maps each step — use a 'matches' counter for updates. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.

Sliding Window Pattern