Permutation in String

medium

Check if one string contains a permutation of another

Permutation in String

Key Insight

Fixed window of s1.length over s2. Window is a permutation when character frequencies match

Step 1Build Target Frequency
s1 = 'ab'Need: a:1, b:1
e
0
i
1
d
2
b
3
a
4
o
5
o
6
o
7

Count character frequencies of s1 to know what to match.

1 / 5
Practice the Code

Step-by-Step Walkthrough: Permutation in String

Fixed window of s1.length over s2. Window is a permutation when character frequencies match

  1. Build Target Frequency

    Count character frequencies of s1 to know what to match.

  2. First Window

    Check the first window of size 2.

  3. Slide Window

    Slide the window and check frequencies again.

  4. Found Match!

    Window contains b and a — frequencies match s1.

  5. Permutation Found

    A permutation of s1 exists in s2.