Permutation in String
Difficulty: Medium
Topics: Hash Table
, Two Pointers
, String
, Sliding Window
Given two strings s1
and s2
, return true
if s2
contains a permutation[^1] 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:
<code>1 <= s1.length, s2.length <= 10
4s1
ands2
consist of lowercase English letters.
Hint:
- 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?
[^1]: Permutation A permutation is a rearrangement of all the characters of a string.
Solution:
We can use the sliding window technique with a frequency counter, rather than trying every possible substring permutation (which would be too slow for large inputs).
Key Idea:
- If a permutation of
s1
is a substring ofs2
, both strings must have the same character frequencies for the letters present ins1
. - We can use a sliding window of length
s1
overs2
to check if the substring within the window is a permutation ofs1
.
Steps:
- Create a frequency counter for
s1
(i.e., count the occurrences of each character). - Slide a window over
s2
of size equal tos1
and check if the frequency of characters in the window matches the frequency of characters ins1
. - If the frequencies match, it means that the substring in the window is a permutation of
s1
.
Algorithm:
- Create a frequency array (
count1
) fors1
. - Use another frequency array (
count2
) for the current window ins2
. - Slide the window over
s2
and update the frequency array for the window as you move. - If the frequency arrays match at any point, return
true
. - If you slide through all of
s2
without finding a match, returnfalse
.
Let’s implement this solution in PHP: 567. Permutation in String
<?php
/**
* @param String $s1
* @param String $s2
* @return Boolean
*/
function checkInclusion($s1, $s2) {
...
...
...
/**
* go to ./solution.php
*/
}
// Example usage
$s1 = "ab";
$s2 = "eidbaooo";
echo checkInclusion($s1, $s2) ? "true" : "false"; // Output: true
?>
Explanation:
- Frequency Arrays:
- We use two arrays (
count1
fors1
andcount2
for the sliding window ins2
) of size 26, corresponding to the 26 lowercase letters. - Each array keeps track of how many times each character appears.
- We use two arrays (
- Sliding Window:
- We first initialize the frequency of the first window (the first
len1
characters ofs2
) and compare it tocount1
. - Then, we slide the window one character at a time. For each step, we update the frequency array by:
- Adding the character that is entering the window.
- Removing the character that is leaving the window.
- After each slide, we compare the two frequency arrays.
- We first initialize the frequency of the first window (the first
- Efficiency:
- Instead of recalculating the frequency for every new window, we efficiently update the frequency array by adjusting just two characters.
- This gives an overall time complexity of O(n), where
n
is the length ofs2
.
Time and Space Complexity:
- Time Complexity:
O(n)
wheren
is the length ofs2
. We iterate overs2
once, updating the frequency arrays in constant time. - Space Complexity:
O(1)
, as we are only using fixed-size arrays (size 26) to store the frequency counts of characters.
This approach ensures that the solution is efficient even for larger inputs.
Read More
- 564 Find the Closest Palindrome
- 552 Student Attendance Record II
- How to Set Up a PHP Development Environment in VS Code with Docker Desktop A Step-by-Step Guide
- php.ini Overview: Boost Performance, Security, and Flexibility
- Comprehensive Methods to Display Arrays in PHP and Laravel
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me: