Permutation in String

Difficulty: Medium

Given strings s1 and s2, check whether s2 contains a permutation of s1 as a contiguous substring: a run with exactly the same letters and counts as s1, in any order.

Return true if it does, and false otherwise.

Examples

Input: s1 = "ab", s2 = "eidbaooo"
Output: true

"ba" is a substring of s2, and it is a rearrangement of "ab".

Input: s1 = "ab", s2 = "eidboaoo"
Output: false

No contiguous substring of s2 has exactly one "a" and one "b".

Input: s1 = "adc", s2 = "dcda"
Output: true

"dca" (positions 0-2 of s2) uses the same letters as "adc".

Constraints

  • 1 <= s1.length, s2.length <= 10^4

  • s1 and s2 consist of lowercase English letters.

Approach

A permutation just rearranges letters, so the question is: does s2 have a window, the length of s1, with the same letter counts as s1?

Brute force builds fresh counts for every window and compares them. That's wasteful, since neighboring windows overlap almost entirely.

A sliding window fixes this. Count s1's letters once, and the first window of s2. Each slide removes one letter on the left and adds one on the right, so you update the counts instead of recomputing them. After each slide, compare to s1's counts; a match means you found a permutation.

Solutions

Brute Force — Recount Every Window

For every window of s2 the length of s1, build its letter counts from scratch and compare them to s1's counts.

function checkInclusion(s1, s2) {
  const need = buildCounts(s1);
  const windowSize = s1.length;

  for (let start = 0; start + windowSize <= s2.length; start++) {
    const have = buildCounts(s2.slice(start, start + windowSize));
    if (sameCounts(need, have)) {
      return true;
    }
  }

  return false;
}

function buildCounts(str) {
  const counts = {};
  for (const ch of str) {
    counts[ch] = (counts[ch] || 0) + 1;
  }
  return counts;
}

function sameCounts(a, b) {
  const keys = new Set([...Object.keys(a), ...Object.keys(b)]);
  for (const key of keys) {
    if ((a[key] || 0) !== (b[key] || 0)) {
      return false;
    }
  }
  return true;
}

Time: O(n * m) — n windows, each costing O(m) to build and compare, m = s1.length · Space: O(26) per window — constant, one count per lowercase letter

Optimal — Fixed-Size Sliding Window

Keep one count array for the current window in s2, update it as the window slides, and compare it to s1's counts after each slide.

function checkInclusion(s1, s2) {
  if (s1.length > s2.length) return false;

  const need = new Array(26).fill(0);
  const have = new Array(26).fill(0);
  const base = "a".charCodeAt(0);

  for (let i = 0; i < s1.length; i++) {
    need[s1.charCodeAt(i) - base]++;
    have[s2.charCodeAt(i) - base]++;
  }

  if (matches(need, have)) return true;

  for (let right = s1.length; right < s2.length; right++) {
    have[s2.charCodeAt(right) - base]++;
    const left = right - s1.length;
    have[s2.charCodeAt(left) - base]--;

    if (matches(need, have)) return true;
  }

  return false;
}

function matches(a, b) {
  for (let i = 0; i < 26; i++) {
    if (a[i] !== b[i]) return false;
  }
  return true;
}

Time: O(n + m) — n = s2.length, m = s1.length, since the letter comparison is a constant 26 slots · Space: O(26) — constant, two fixed-size count arrays