1.8 - Permutation in a String (hard)
Problem:
Given two strings s1
and s2
, return true if s2
contains the permutation of s1
.
In other words, one of s1
's permutations is the substring of s2
.
Example 1:
Example 2:
Constraints:
1 <= s1.length, s2.length <= 104
s1
ands2
consist of lowercase English letters.
Solution:
Sliding Window
Algorithm
Instead of generating the hashmap afresh for every window considered in s2, we can create the hashmap just once for the first window in s2. Then, later on when we slide the window, we know that we remove one preceding character and add a new succeeding character to the new window considered. Thus, we can update the hashmap by just updating the indices associated with those two characters only. Again, for every updated hashmap, we compare all the elements of the hashmap for equality to get the required result.
Complexity Analysis
Time complexity : O(l1+26*(l2-l1)), where l1 is the length of string s1 and l2 is the length of string s2.
Space complexity : O(1). Constant space is used.
Optimized Sliding Window:
Algorithm
The last approach can be optimized, if instead of comparing all the elements of the hashmaps for every updated s2map corresponding to every window of s2 considered, we keep a track of the number of elements which were already matching in the earlier hashmap and update just the count of matching elements when we shift the window towards the right.
To do so, we maintain a count variable, which stores the number of characters(out of the 26 alphabets), which have the same frequency of occurence in s1 and the current window in s2. When we slide the window, if the deduction of the last element and the addition of the new element leads to a new frequency match of any of the characters, we increment the count by 1. If not, we keep the count intact. But, if a character whose frequency was the same earlier(prior to addition and removal) is added, it now leads to a frequency mismatch which is taken into account by decrementing the same count variable. If, after the shifting of the window, the count evaluates to 26, it means all the characters match in frequency totally. So, we return a True in that case immediately.
Complexity Analysis
Time complexity : O(l1+(l2−l1)). where l1 is the length of string s1 and l2 is the length of string s2.
Space complexity : O(1). Constant space is used.
Last updated