Given two strings
The testcases will be generated such that the answer is unique.
Output: "BANC"
Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.
Output: "a"
Explanation: The entire string s is the minimum window.
Output: ""
Explanation: Both 'a's from t must be included in the window.
Since the largest window of s only has one 'a', return empty string.
Constraints:
m == s.length n == t.length 1 <= m, n <= 105 s andt consist of uppercase and lowercase English letters.
Follow up: Could you find an algorithm that runs in
Contents
Solution 1 - Using sliding window technique, keep track of minimum window size where characters in 't' found in 's'
In this approach, first we are going to capture characters in
Implementation steps:
-
First, go through characters of input string
t , and store the characters and their count into aHashMap , call ittMap . -
Next, create following variables:
-
Create a
HashMap ,sMap that we will use it to store characters of input strings and their count. -
left andright that we will use for the sliding window. -
requiredCount andhaveCount , initializerequiredCount with size oftMap andhaveCount with 0, we will use these variables to check where we have the required count of each characters int are found ins . -
minLength to capture minimum length sliding window found so far, initialize this withInteger.MAX_VALUE since we are trying to find minimum length, andansIndices integer array with two value to store the indices ofleft andright indices where minimum length sliding window found.
-
Create a
-
Next, run a
while loop untilright pointer reaches end of input strings , and do the following:-
Take the current character from
s , and add it to thesMap . -
Next, check if the current character is present in
tMap and the count of that character is same intMap andsMap , if they are same incrementhaveCount , since this character satisfies the condition of matching count in botht and current window ofs . -
Next, run a
while loop untilrequiredCount andhaveCount , if we find a sliding window where characters count int matched current window ofs , then we want to capture the length of that substring, and moving the slide the next characters until the condition is break again. -
If the current window length
right - left +1 is less thanminLength , then replaceminLength with current window length. Also, store current indicesleft andright intoansIndices array. -
Now, take the character at
left pointer, and decrement its count insMap by 1, and if this character is present intMap and the current count insMap falls below of count intMap , then decrementhaveCount . -
Then, increment
left pointer to point to next character on left side in current window.
-
Take the current character from
-
Next, increment
right pointer to move to next character. -
Finally, check if there is any minimum value found, i.e. check if
minLength is still has the initialized value (Integer.MAX_VALUE ), if it is then return an empty string"" , otherwise return substring using the indices fromansIndices (note to use +1 on right index, since right index exclusive).
Complexity Analysis:
Time complexity: Above code runs in O(n) time, where
Space complexity: O(n), since we are storing characters of input strings
Above implementations source code can be found at