Skip to content
DSA Patterns
Menu
Language
Sliding WindowLC #5675 of 6← PreviousNext →

Demo preview: These solutions pass the automated test suite but have not been human-reviewed. Treat trade-offs and prose as draft.

Permutation in String

Problem (restated)

Return true if s2 contains a permutation of s1 as a contiguous substring.

Intuition

A permutation of s1 is any window of length |s1| with the same character counts.

Approaches

Fixed window frequency match

Unverified
Time O(n)Space O(1)

Idea. Count s1 frequencies. Slide a window of that length over s2, compare counts (or maintain a matches counter).

Walkthrough. s1=“ab”, s2=“eidbaooo”. Window “ba” matches counts of a,b → true.

Trade-offs. O(1) alphabet space for lowercase English. Sorting every window is slower.

Solution
export function checkInclusion(s1: string, s2: string): boolean {
  if (s1.length > s2.length) return false;
  const need = new Array<number>(26).fill(0);
  const win = new Array<number>(26).fill(0);
  for (let i = 0; i < s1.length; i++) {
    need[s1.charCodeAt(i)! - 97]!++;
    win[s2.charCodeAt(i)! - 97]!++;
  }
  const eq = () => need.every((v, i) => v === win[i]);
  if (eq()) return true;
  for (let i = s1.length; i < s2.length; i++) {
    win[s2.charCodeAt(i)! - 97]!++;
    win[s2.charCodeAt(i - s1.length)! - 97]!--;
    if (eq()) return true;
  }
  return false;
}
export function checkInclusion(s1: string, s2: string): boolean {
  if (s1.length > s2.length) return false;
  const need = new Array<number>(26).fill(0);
  const win = new Array<number>(26).fill(0);
  for (let i = 0; i < s1.length; i++) {
    need[s1.charCodeAt(i)! - 97]!++;
    win[s2.charCodeAt(i)! - 97]!++;
  }
  const eq = () => need.every((v, i) => v === win[i]);
  if (eq()) return true;
  for (let i = s1.length; i < s2.length; i++) {
    win[s2.charCodeAt(i)! - 97]!++;
    win[s2.charCodeAt(i - s1.length)! - 97]!--;
    if (eq()) return true;
  }
  return false;
}

Template connection

Fixed window whose length is len(s1). The window counts must match s1. Each slide drops the character that leaves and takes the character that enters. The first matching window answers true.

Reflection

  • A window fixed at s1.length is how you hear “permutation”. Need/have counts, or a 26-wide difference?
  • When the window slides, forgetting to put back the character that left produces which false positive?
  • If s1 is longer than s2, do you return false immediately?
Contributor review checklist

Interactive

Mental model

A worked animation for this problem. Scrub steps or press space to pause; re-tell the invariant out loud.

Step 1 of 6
1/6

Does s2 contain a permutation of s1="ab"? Need a window of length 2 with the same counts.

e
0
i
1
d
2
b
3
a
4
o
5
o
6
o
7
mapa→1b→1

need = ab

Key:CurrentActive rangeMatchSettled