Permutation in String

MediumSliding windowLeetCode 567 ↗World 2-8
0:00 / 0:00

Does any window of s2 hold exactly the letters of s1? Slide a fixed-size window and keep 26 letter counts in step, one letter in and one out, so every check is O(1) and the whole scan is O(n).

▼

The problem

Given two strings s1 and s2, return true if s2 contains a permutation of s1: a contiguous substring with exactly the same letters, in any order.

Examples: s1 = ab, s2 = eidbaooo is true (ba, frames 3–4); s1 = ab, s2 = eidboaoo is false (an o splits the b from the a, and no two neighbours hold one a and one b).

def checkInclusion(s1, s2):
    m = len(s1)
    if m > len(s2):
        return False
    need = [0] * 26
    have = [0] * 26
    for c in s1:
        need[ord(c) - 97] += 1
    for i, c in enumerate(s2):
        have[ord(c) - 97] += 1
        if i >= m:
            have[ord(s2[i - m]) - 97] -= 1
        if have == need:
            return True
    return False
TRY IT ON LEETCODE ▶

The solution

def checkInclusion(s1, s2):
    m = len(s1)
    if m > len(s2):
        return False
    need = [0] * 26
    have = [0] * 26
    for c in s1:
        need[ord(c) - 97] += 1
    for i, c in enumerate(s2):
        have[ord(c) - 97] += 1
        if i >= m:
            have[ord(s2[i - m]) - 97] -= 1
        if have == need:
            return True
    return False

Transcript

Permutation in String. You get two strings. Return true if the second one contains a permutation of the first: the same letters, in any order, sitting side by side.

Take A, B, and E, I, D, B, A, O, O, O. B, A sit side by side, a reordering of A, B, so it's true. Now try E, I, D, B, O, A, O, O. An O splits the B from the A: false.

The naive way tries every ordering of the first string and searches for each one. With m letters, that's m factorial orderings: ten letters make over three million. Sorting every window is better, but each slide sorts almost the same letters again.

Here's the key. A permutation has exactly the same letter counts. So count the first string once: one A, one B. Then slide a window of exactly that length along the second string, counting the letters inside it.

Each slide changes only two counts. The letter entering on the right goes up by one. The letter leaving on the left goes down by one. When every count matches, we've found a permutation.

In code, keep two lists of twenty six counts. Count the first string into one of them. For each letter of the second, add it to the window, and once the window is too long, subtract the letter that fell out. If the two lists are equal, return true.

Let's run it. The window fills with E and I: no match. Slide: D enters, E leaves. I, D: still no. B enters, I leaves. D, B: a B, but no A. A enters, D leaves. B, A: one A, one B. A match, so it's true.

Each letter enters once and leaves once, and comparing twenty six counts is constant work, so the time is order n. We only keep two lists of twenty six counts, however long the strings get, so the space is order one.

Count the first string, slide a fixed window, compare the counts. That's Permutation in String.