The Indian Engineer

Problem 567 Permutation in String

Posted on 6 mins

Hash-Table Two-Pointers String Sliding Window

Problem Statement

Link - Problem 567

Question

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:

Solution

class Solution {
public:
    bool checkInclusion(string s1, string s2) {
        if (s1.length() > s2.length())
            return false;

        vector<int> arr1(26, 0);
        vector<int> arr2(26, 0);

        for (int i = 0; i < s1.length(); i++) {
            arr1[s1[i] - 'a']++;
            arr2[s2[i] - 'a']++;
        }

        for (int i = 0; i < s2.length() - s1.length(); i++) {
            if (matches(arr1, arr2))
                return true;
            arr2[s2[i + s1.length()] - 'a']++;
            arr2[s2[i] - 'a']--;
        }

        return matches(arr1, arr2);
    }

    bool matches(const vector<int>& arr1, const vector<int>& arr2) {
        for (int i = 0; i < 26; i++) {
            if (arr1[i] != arr2[i])
                return false;
        }
        return true;
    }
};

Complexity Analysis

| Algorithm      | Time Complexity | Space Complexity |
| -------------- | --------------- | ---------------- |
| Sliding Window | O(n)            | O(1)             |

Explanation

Intial Thoughts

To solve this problem, we should think about how to efficiently compare all substrings of s2 with the same length as s1. We should consider using a sliding window approach to scan through s2 and compare each substring with s1. We should also think about how to quickly determine if two strings are permutations of each other, such as by using a frequency count of characters. Additionally, we should check if s1 is longer than s2, in which case we can immediately return false. Furthermore, we should consider the constraints of the problem, such as the lengths of s1 and s2, and the fact that they only contain lowercase English letters.

Intuitive Analysis

Intuitively, we can solve this problem by using a sliding window to scan through s2 and compare each substring with s1. We can use two frequency arrays to count the characters in s1 and the current substring of s2. We can then compare these two arrays to determine if the current substring is a permutation of s1. If it is, we can return true. We should also consider the edge case where s1 is longer than s2, and handle it accordingly. Moreover, we should think about how to efficiently update the frequency array for the sliding window, such as by incrementing the count for the new character and decrementing the count for the old character. We should also consider using a helper function to compare the two frequency arrays, to make the code more readable and maintainable.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Obviously, brute force will result in TLE. Think of something else.

How will you check whether one string is a permutation of another string?

One way is to sort the string and then compare. But, Is there a better way?

If one string is a permutation of another string then they must have one common metric. What is that?

Both strings must have same character frequencies, if one is permutation of another. Which data structure should be used to store frequencies?

What about hash table? An array of size 26?


Similar Questions:

Title URL Difficulty
Minimum Window Substring https://leetcode.com/problems/minimum-window-substring Hard
Find All Anagrams in a String https://leetcode.com/problems/find-all-anagrams-in-a-string Medium