Given two strings
In other words, return
Output: true
Explanation: s2 contains one permutation of s1 ("ba").
Output: false
Constraints:
0 <= s1.length, s2.length <= 104 s1 ands2 consist of lowercase English letters.
Contents
Solution 1 - Using sliding window technique, and HashMap or Array to store character count Solution 2 - Using sliding window technique, and match the count of characters in-line
In this approach, we are going to apply sliding window technique, using two pointers
Implementation steps:
-
First, we will create four variables
left ,right that we will use to adjust the window, and twoHashMap ss1_count to hold characters count froms1 , ands2_count to hold the characters count ins2 . -
Next, loop through input string
s1 characters and updates1_count , if the character already exists, then increment its count by 1, otherwise add the character with count as 1. -
Next, run a
while loop untilright pointer reaches end of array, and do the following:-
Read the character at
right index, and update thes2_count , if the character already exists, then increment its count by 1, otherwise add the character with count as 1. -
Then, check if the
right is going out of bounds of the window sizeright >= s1.length() (>= is because indexes starts with 0, and if the current index is same ass1 's length, that means we have read more elements thans1 's length), at this time we will decrement the count of character atleft index, and if the count had become 0, then remove that character froms2_count .
For example:s1 = "ab" ands2 = "eidbaooo" , here s1's length is 2, so the window size is 2. When we move to character 'd' in "eidbaooo", we will be going of out of bounds of window size, so we will move the left index to 'i' to keep the window size as 2. -
Next, check if both
s1_count ands2_count have same characters and have same counts, if they are equal, that means we have found a s1's permutation (anagram) in s2, so returntrue . -
At the end of while loop increment
right pointer to move to the next character.
-
Read the character at
-
If no permutation is found, return
false at the end.
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(1), since the input can contain only lowercase English letters, so the
In terms of time complexity of this solution, it is going to be same as above, but we can reduce the constant factor, previous solution time complexity is O (26 * n)
since we are checking character count at every character of
The main intution of this solution is, say if the input strings are
Let's understand this step by step.
-
First, store the character count of
s1 and character count froms2 , just the characters from substring with same length ass1
s1 = "abc" s2 = "abxyzabc"
s1_count =1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
s2_count =1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0
At this step, as you can see there are 24 characters count is matching, whether it is 0 or 1, so thematches = 24 -
Next, we add right character from
s2 and remove the left character from s2, to keep the window size same as s1's length.
s2 = " abxyzabc"
We reduce count of 'a' by -1 and add +1 count to 'y'
s1_count =1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
s2_count =0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0
At this step, as you can see there are 22 characters count is matching, so thematches = 22 -
Next, move the window to the next character.
s2 = " abxyzabc"
We reduce count of 'b' by -1 and add +1 count to 'z'
s1_count =1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
s2_count =0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1
At this step, as you can see there are 20 characters count is matching, so thematches = 20 -
Next, move the window to the next character.
s2 = " abxyzabc"
We reduce count of 'x' by -1 and add +1 count to 'a'
s1_count =1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
s2_count =1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1
At this step, as you can see there are 22 characters count is matching, so thematches = 22 -
Next, move the window to the next character.
s2 = " abxyzabc"
We reduce count of 'y' by -1 and add +1 count to 'b'
s1_count =1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
s2_count =1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
At this step, as you can see there are 24 characters count is matching, so thematches = 24 -
Next, move the window to the next character.
s2 = " abxyzabc"
We reduce count of 'z' by -1 and add +1 count to 'c'
s1_count =1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
s2_count =1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
At this step, as you can see there are 26 characters count is matching, so thematches = 26 -
Since the
matches count is 26, we can returntrue , note that when updatingmatches we will only look at the character that is removed or added to the window.
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(1), since the temporary arrays size is 26.
Above implementations source code can be found at