Home
DSA Patterns
Sliding Window
Permutation in String
Permutation in String (LeetCode 567)
Permutation in String (LeetCode 567): check whether s2 contains a permutation of s1 by sliding a fixed window of len(s1) and comparing letter counts.
Problem Understanding
Permutation in String: return true if s2 contains some permutation of s1 as a contiguous substring.
Examples: s1 = "ab", s2 = "eidbaooo" returns true because "ba" appears in s2. s1 = "ab", s2 = "eidboaoo" returns false.
The Intuition: Same Length, Same Counts
A permutation of s1 has exactly len(s1) letters and exactly the same letter counts as s1. So the question becomes: does any window of s2 with length len(s1) have the same counts as s1?
That is a fixed-size sliding window:
- Count
s1intoneed[26]. - Walk
s2: add the letter entering on the right; once the window is longer thanlen(s1), remove the letter leaving on the left. - If the window's counts equal
need, returntrue.
Sorting each window would work too, but costs O(k log k) per step; updating two counts is O(1). The same counting idea solves Valid Anagram for whole strings.
Permutation in String Visualization
IS A PERMUTATION OF "AB" IN s2? · DIMMED COLUMNS ALREADY AGREE
COUNT s1
0
1
2
3
4
5
6
7
a
b
d
e
i
o
need
window
window end
:
counts equal
false
Answer
:
Count s1's letters: a×1, b×1. The window will hold 2 letters of s2
Next
Add 'e' (index 0): the window is still filling
s2
s1
The window over s2 is shaded, and the letter just added or dropped is filled. The table below counts each letter in s1 and in the window; columns that already agree are dimmed, so you can see exactly which letters are still off.
Permutation in String Dry Run
Input: s1 = "ab", s2 = "eidbaooo"
| i | Window | Counts equal? |
|---|---|---|
| 1 | ei | no |
| 2 | id | no |
| 3 | db | no |
| 4 | ba | yes |
The window "ba" matches, so the answer is true.
Permutation in String LeetCode Solution
Two count arrays and a fixed window of len(s1).
Permutation in String Code
1var checkInclusion = function(s1, s2) {2 if (s1.length > s2.length) return false;3 const need = new Array(26).fill(0), window = new Array(26).fill(0);4 for (const c of s1) need[c.charCodeAt(0) - 97]++;5 for (let i = 0; i < s2.length; i++) {6 window[s2.charCodeAt(i) - 97]++;7 if (i >= s1.length) window[s2.charCodeAt(i - s1.length) - 97]--;8 if (window.every((v, k) => v === need[k])) return true;9 }10 return false;11};
Permutation in String in Python, Java, C++, TypeScript and C
Permutation in String in Python
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
if len(s1) > len(s2):
return False
need, window = [0] * 26, [0] * 26
for c in s1:
need[ord(c) - 97] += 1
for i, c in enumerate(s2):
window[ord(c) - 97] += 1
if i >= len(s1):
window[ord(s2[i - len(s1)]) - 97] -= 1
if window == need:
return True
return FalsePermutation in String in Java
class Solution {
public boolean checkInclusion(String s1, String s2) {
if (s1.length() > s2.length()) return false;
int[] need = new int[26], window = new int[26];
for (char c : s1.toCharArray()) need[c - 'a']++;
for (int i = 0; i < s2.length(); i++) {
window[s2.charAt(i) - 'a']++;
if (i >= s1.length()) window[s2.charAt(i - s1.length()) - 'a']--;
if (Arrays.equals(window, need)) return true;
}
return false;
}
}Permutation in String in C++
class Solution {
public:
bool checkInclusion(string s1, string s2) {
if (s1.size() > s2.size()) return false;
vector<int> need(26, 0), window(26, 0);
for (char c : s1) need[c - 'a']++;
for (int i = 0; i < (int)s2.size(); i++) {
window[s2[i] - 'a']++;
if (i >= (int)s1.size()) window[s2[i - s1.size()] - 'a']--;
if (window == need) return true;
}
return false;
}
};Permutation in String in TypeScript
function checkInclusion(s1: string, s2: string): boolean {
if (s1.length > s2.length) return false;
const need: number[] = new Array(26).fill(0), window: number[] = new Array(26).fill(0);
for (const c of s1) need[c.charCodeAt(0) - 97]++;
for (let i = 0; i < s2.length; i++) {
window[s2.charCodeAt(i) - 97]++;
if (i >= s1.length) window[s2.charCodeAt(i - s1.length) - 97]--;
if (window.every((v, k) => v === need[k])) return true;
}
return false;
}Permutation in String in C
bool checkInclusion(char* s1, char* s2) {
int n1 = strlen(s1), n2 = strlen(s2);
if (n1 > n2) return false;
int need[26] = {0}, window[26] = {0};
for (int i = 0; i < n1; i++) need[s1[i] - 'a']++;
for (int i = 0; i < n2; i++) {
window[s2[i] - 'a']++;
if (i >= n1) window[s2[i - n1] - 'a']--;
if (!memcmp(window, need, sizeof(need))) return true;
}
return false;
}Edge Cases & Common Mistakes
- s1 longer than s2: no window fits; return
falsefirst. - Repeated letters: counts, not sets, are compared;
"aab"needs two a's in the window. - Removing the wrong letter: the letter leaving is
s2[i − len(s1)], oncei ≥ len(s1). - Comparing only after the window is full: comparing earlier is harmless, because a partial window can never equal
need.
Summary
A permutation of s1 is a window of s2 with length len(s1) and the same letter counts. Slide that window across s2, adding the letter on the right and removing the one on the left, and compare the two 26-letter count arrays after each step. O(n) time, O(1) space.
More practice: DSA interview questions with answers · patterns cheat sheet
