Skip to content
AI360Xpert
Beta
Difficulty: MediumSliding Window

Permutation in String

Problem in Plain English

Given strings s1 and s2, return true when some contiguous substring of s2 is a rearrangement of s1, using exactly the same multiset of characters.

Problem Statement

Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise. In other words, return true if one of s1 permutations is a substring of s2.

A permutation keeps the exact multiset of letters, so the task equals asking whether some window of s2 with length s1.length has identical letter counts to s1. For s1 = "ab" and s2 = "eidbaooo" the window "ba" matches, so the answer is true.

Constraints

  • 1 <= s1.length, s2.length <= 10^4
  • s1 and s2 consist of lowercase English letters.

Examples

Example 1

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

The substring "ba" at indices 3 to 4 is a permutation of "ab".

Example 2

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

No length-2 window of s2 holds exactly one "a" and one "b".

Example 3

Input
s1 = "abc", s2 = "ab"
Output
false

The required length is 3 but the text has only 2 characters, so no candidate window exists.

Example 4

Input
s1 = "aab", s2 = "aaaba"
Output
true

The first window aaa has three a letters and no b, so it fails. The next window aab at indices 1 through 3 has exactly two a letters and one b, so it succeeds; merely finding both distinct letters would not suffice.

Intuition

A permutation changes order but preserves how many copies of each letter exist. A window is a contiguous group of characters; any candidate here must have exactly m = s1.length characters. In Example 1, ab needs one a and one b, so ba succeeds even though the order differs. Counting letters avoids generating permutations. Neighbouring windows share all but their entering and departing characters, letting us update the counts instead of recounting them.

Approaches

Brute Force

Solution Details

Reveal Brute Force: intuition, complexity, and code

Hints

Hint 1
Two strings are permutations of each other exactly when their letter counts agree on all 26 letters.
Hint 2
Any matching substring must sit in one window of length s1.length, so only those fixed-size windows need checking.
Hint 3
Slide that window one step at a time: each step adds one letter on the right and drops one on the left, so the counts update in constant time.

Edge Cases

  • s1 is longer than s2: no window of the required size exists, so the answer is false.
  • Single equal letters such as s1 = "a" and s2 = "a": the first window already matches and the answer is true.
  • s2 is all one repeated letter, such as s1 = "ab" with s2 = "aaaa": counts never align and the answer is false.
  • Equal-length strings have only the initial window; the final matches check handles both success and failure.
  • Repeated multiplicities matter: aab does not match abb despite having the same distinct letters.
  • The last window may be the only match: ab inside oooba must return true.
  • When the entering and departing letters are equal, perform both updates; the final count and matches value must return to their previous state.

Common Mistakes and Interview Tips

  • Comparing distinct-letter sets loses multiplicities: aab and abb have the same set but are not permutations.
  • Updating matches every time a count changes, rather than only when equality is created or destroyed, corrupts the invariant.
  • Checking only at the start of slide iterations misses the final window; compare matches after the loop too.
  • Using a variable-size window answers a different question: every candidate must have exactly s1.length characters.
  • Interview tip: explain why absent letters with counts zero still contribute to the 26 matches, then trace an add and a drop separately.

Key Takeaway

For anagram or permutation checks inside a string, hold one fixed-size window and update its frequency counts incrementally instead of rebuilding them.