Skip to main content

Permutation in String

Check if s2 contains a permutation of s1 Example: s1 = "ab", s2 = "eidbaooo" => true (contains "ba")

Given non-empty strings s1 and s2 containing only lowercase English letters, consider contiguous substrings of s2 whose length equals the length of s1. Part 1 returns a boolean indicating whether any such substring is a rearrangement of all the characters in s1. Part 2 returns an integer array of every matching substring start index, using zero-based indices in increasing order and allowing overlapping matches. Part 3 returns the minimum number of character substitutions within any such substring needed to make it a rearrangement of s1. If s1 is longer than s2, return false in Part 1, an empty array in Part 2, and -1 in Part 3.

Examples

You write

check_inclusion(s1: str, s2: str)

  1. Example 1

    Input
    s1 = "ab", s2 = "eidbaooo"
    returns
    true
  2. Example 2

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

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Sliding Window

I reach for it when

  • The question asks for the longest, shortest or best stretch of a sequence.
  • A rule like "at most k" or "no repeats" decides what counts as valid.

Say this before you type. Say what makes the window valid, and what you do to the left edge when it breaks.

All 9 Sliding Window problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Part 1: Permutation in Stringmedium
2Part 2
3Part 3

Each locked part opens in the editor once the part before it passes.