2Brute2Best
🔥0

Permutation in String

Medium·LeetCode #567 Pro OnlyMedium
LeetCode
01

Problem Statement

You are given two strings s1 and s2.

Return true if some rearrangement of s1 (a permutation) appears inside s2 as a contiguous substring. Otherwise return false.

In other words: is there a substring of s2 that uses exactly the same letters as s1, the same number of times each, in any order?

Input

Two strings s1 and s2 of lowercase English letters.

Output

true if a permutation of s1 is a substring of s2, otherwise false.

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

s2 contains "ba" (positions 3–4), which is a rearrangement of "ab".

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

There is a 'b' and an 'a' in s2, but they are never next to each other, so no length-2 substring has exactly one 'a' and one 'b'.

Constraints
  • • 1 ≤ s1.length, s2.length ≤ 10^4
  • • s1 and s2 contain only lowercase English letters
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“permutation of s1”
→ Order doesn't matter, only letter counts → compare count arrays.
“substring of s2”
→ Contiguous range of s2 → window.
“(implied) same length as s1”
→ A permutation keeps the length, so the window size is fixed at s1.length.
03

Approach Complexity Ladder

Brute Force — count every window from scratchO(N · M)O(1)

For each length-m window, build a fresh count array and compare

Better — slide the counts, compare 26 each stepO(26 · N)O(1)

Update two counts per slide, then compare the two 26-length arrays

Optimal — fixed window + matches counterO(N + M)O(1)

Track how many of the 26 letters already have the right count

Brute Force Intuition

A permutation of s1 must have length m = s1.length. So check every substring of s2 of length m: count its letters from scratch and compare with the counts of s1.

Recognition rule for this sub-pattern:

Window size is fixed at K. Each slide adds one element on the right and removes one on the left — update the aggregate in O(1).
Remember this · Sliding Window
Trigger
Contiguous subarray / substring + longest, shortest, maximum, count, or "of size K".
Think
Can I keep a valid range and move it, instead of checking every subarray from scratch?
Key question
When is my current window valid?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.