#567

Permutation in String

Medium Fixed window + counts

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Decide whether some rearrangement of s1 appears as a contiguous substring of s2.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: does some rearrangement of s1 appear inside s2?

Why this works: a rearrangement has the same letter counts. Slide a window of length |s1| over s2 and compare counts.

Example: s1 = "ab", s2 = "eidbaooo"

"ei", "id", "db" → no"ba" → counts a1 b1 match → true

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Generating all permutations of s1 — factorial time.

Solve on LeetCode ↗
Complexity
  • Time: O(|s1| + |s2|).
  • Space: O(26).
Topic: Sliding Window
🎬 Episode 10: video coming soon