Advertisement

Permutation in String - LeetCode 567 Solution

Permutation in String - Complete Solution Guide

Permutation in String is LeetCode problem 567, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 's permutations is the substring of s2 . Example 1: Input: s1 = "ab", s2 = "eidbaooo" Output: true Explanation: s2 contains one permutation of s1 ("ba"). Example 2: Input: s1 = "ab", s2 = "eidboaoo" Output: false Constraints: 1 <= s1.length, s2.length <= 10 4 s1 and s2 consist of lowercase English letters.

Detailed Explanation

The problem asks us to determine if a given string `s2` contains any permutation of another string `s1` as a substring. A permutation of `s1` is any arrangement of its characters. So, we need to check if any rearrangement of the characters in `s1` exists consecutively within `s2`. The input consists of two strings, `s1` and `s2`, containing lowercase English letters. The output is a boolean value: `true` if a permutation of `s1` is found within `s2`, and `false` otherwise. The constraints specify that the lengths of `s1` and `s2` are between 1 and 10,000, inclusive.

Solution Approach

The provided solution employs a sliding window technique combined with character frequency counting. First, it checks if the length of `s1` exceeds that of `s2`. If it does, no permutation of `s1` can exist in `s2`, and the function returns `false`. Otherwise, it creates two frequency arrays (or hash maps) `s1_map` and `s2_map` to store the character counts for `s1` and the initial substring of `s2` with length equal to `s1`, respectively. It then compares these two arrays. If they are identical, it means that the initial substring of `s2` is a permutation of `s1`, and the function returns `true`. If not, it slides the window over `s2` by one character at a time. In each step, it updates `s2_map` by adding the frequency of the new character entering the window and subtracting the frequency of the character leaving the window. After each update, it compares `s1_map` and `s2_map` again. If they match at any point, the function returns `true`. If the loop completes without finding a match, it means that no permutation of `s1` exists in `s2`, and the function returns `false`.

Step-by-Step Algorithm

  1. Step 1: Check if `len(s1)` > `len(s2)`. If true, return `false` because a permutation of a longer string cannot be a substring of a shorter string.
  2. Step 2: Initialize two frequency arrays, `s1_map` and `s2_map`, of size 26 (for the 26 lowercase English letters).
  3. Step 3: Populate `s1_map` with the character frequencies of `s1`.
  4. Step 4: Populate `s2_map` with the character frequencies of the first `len(s1)` characters of `s2`.
  5. Step 5: Compare `s1_map` and `s2_map`. If they are equal, return `true` because the initial substring of `s2` is a permutation of `s1`.
  6. Step 6: Iterate from `i = len(s1)` to `len(s2) - 1`. In each iteration:
  7. Step 7: Increment the frequency of `s2[i]` in `s2_map` and decrement the frequency of `s2[i - len(s1)]` in `s2_map`. This is the sliding window update.
  8. Step 8: Compare `s1_map` and `s2_map`. If they are equal, return `true`.
  9. Step 9: If the loop completes without finding a match, return `false`.

Key Insights

  • Insight 1: The core idea is to recognize that we don't need to explicitly generate all permutations of `s1`. Instead, we can use a sliding window approach on `s2` and compare the character frequencies within the window to the character frequencies in `s1`.
  • Insight 2: A hash table (or a fixed-size array in this case, given the limited character set) is an efficient way to store and compare character frequencies.
  • Insight 3: If the length of `s1` is greater than `s2`, there can't be any permutation of `s1` inside `s2`. This is an important initial check for optimization.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Hash Table, Two Pointers, String, Sliding Window.

Companies

Asked at: Agoda, Cisco, Expedia, Goldman Sachs, Revolut, Yandex.