Coding interview problems › Sliding Window
🪟 Sliding Window Medium
Permutation in String
Permutation in String explained: slide a fixed window of len(s1) over s2 and compare letter counts in O(n). Anagram-in-window trick, tested Python, live visual.
Also searched as: check if string contains permutation of another · find anagram substring sliding window
Try it live Free
Sliding Window
Slide a window of fixed size and update it with one entering and one leaving element per step. Here the window holds letter counts instead of a sum.
Also useful: Hash Tables
The problem
Given two strings s1 and s2, return True if some contiguous substring of s2 is a rearrangement (permutation) of s1.
"ba" is in s2.
The key insight
A permutation of s1 is any string with the same letter counts and the same length. So slide a window of exactly len(s1) over s2, keeping its letter counts up to date by adding the entering letter and removing the leaving one, and check whether the counts match.
Step-by-step approach
- If
len(s1) > len(s2), returnFalse. - Build counts for
s1and for the first window ofs2. - If they match, return
True. - Slide the window one step: add
s2[i], removes2[i - m], and compare again. - Return
Falseif no window matched.
Python solution
def check_inclusion(s1, s2):
m = len(s1)
if m > len(s2):
return False
need = [0] * 26
win = [0] * 26
for ch in s1:
need[ord(ch) - 97] += 1
for i, ch in enumerate(s2):
win[ord(ch) - 97] += 1 # letter entering
if i >= m:
win[ord(s2[i - m]) - 97] -= 1 # letter leaving
if win == need:
return True
return FalseComplexity
Each step does O(1) count updates plus a 26-element comparison, so it is linear in len(s2). A matches counter can make each step O(1).
Common mistakes
- Comparing counts before the window has reached full length, which can match a shorter prefix (the code above is safe because a short window cannot have the same total count).
- Removing
s2[i - m + 1]instead ofs2[i - m], an off-by-one that keeps the window one letter short. - Using a set comparison, which ignores letter counts.
Frequently asked questions
What is the time and space complexity of Permutation in String?
O(26 · n), effectively O(n), where n is the length of s2, and O(1) space for two 26-letter count arrays.
How do I find all starting indices of anagrams instead?
Use the same window, but instead of returning True, append i - m + 1 every time the counts match.
Can I avoid comparing all 26 counts each step?
Yes. Keep a count of how many letters currently have matching counts, and update it only for the entering and leaving letters.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Sliding Window Sliding Window lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.