|
| 1 | +**Description:** |
| 2 | +Given two strings `str` and `subStr` of lengths `m` and `n` respectively, return the minimum window |
| 3 | +substring of `str` such that every character in `subStr` (including duplicates) is included in the window. If there is no such substring available, just return the empty string(i.e,""). |
| 4 | + |
| 5 | +### Examples |
| 6 | +Example 1: |
| 7 | + |
| 8 | +Input: str = "ADOBECODEBANC", subStr = "ABC" |
| 9 | +Output: "BANC" |
| 10 | +Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t. |
| 11 | + |
| 12 | +Example 2: |
| 13 | +Input: str = "A", subStr = "A" |
| 14 | +Output: "A" |
| 15 | + |
| 16 | +Example 3: |
| 17 | +Input: str = "a", subStr = "aa" |
| 18 | +Output: "" |
| 19 | + |
1 | 20 | **Algorithmic Steps**
|
2 |
| -This problem is solved with the help of sliding window approach using two hash maps. The algorithmic approach can be summarized as follows: |
| 21 | +This problem is solved with the help of **sliding window** approach using two hash maps. The algorithmic approach can be summarized as follows: |
3 | 22 |
|
4 | 23 | 1. Add a preliminary corner case condition by checking the second substring is empty or not. If it is empty, then return the empty substring as minimum window substring.
|
5 | 24 |
|
6 |
| -2. Initialize two HashMaps, which store unique characters and their respective character count for the given two substrings. |
| 25 | +2. Initialize two HashMaps(`windowStrCount` & `subStrCount`), which store unique characters and their respective character count for their given substrings. |
7 | 26 |
|
8 |
| -3. Initialize minLength variable to max integer value, to store the length of the minimum window substring. |
| 27 | +3. Initialize `minLength` variable with maximum integer value, which is used to store the length of the minimum window substring. |
9 | 28 |
|
10 |
| -4. Initialize left(leftIndex) and right(rightIndex) indexes to -1, to keep track of the minimum substring window boundaries. |
| 29 | +4. Initialize substring boundaries(`subStrBoudaries`) variable to [-1, -1], which is used to keep track of the minimum substring window boundaries. |
11 | 30 |
|
12 |
| -5. Iterate over the second substring and count the character count using hashmap. |
| 31 | +5. Iterate over the second substring and count the character count using respective hashmap. |
13 | 32 |
|
14 |
| -6. Initialize total character count(i.e, `required`) from second substring to max integer value and total character count(i.e, `having`) of first substring based on first substring to 0. |
| 33 | +6. Initialize second substring's required character count(i.e, `required`) to the size of second hashmap and total character count(i.e, `having`) of first substring to 0. |
15 | 34 |
|
16 | 35 | 7. Initialize both left and right pointers to 0, to keep track of the current window substring.
|
17 | 36 |
|
18 |
| -8. Iterate through second substring using right pointer until the end of string. |
| 37 | +8. Iterate through first substring using right pointer until the end of string. |
19 | 38 |
|
20 |
| -9. If the current character from first substring exists in second substring hashmap, update the character counter of first hashmap. Also, increment `having` variable if the character count in both hashmaps are equal. |
| 39 | +9. If the current character from first substring exists in second substring hashmap, update the character count of first hashmap. Also, increment `having` variable if the character count in both hashmaps are equal. |
21 | 40 |
|
22 |
| -10. Until `required` and `having` variables are equal, first calculate the left and right indexes along with `minLength` variable. Thereafter, move/increment the left pointer and decrement the character count in first hashmap to find the minimum possible substring window. If the current left character found in second hashmap and first hashmap character count is less than second one, `having` variable needs to be decremented. |
| 41 | +10. Iterate a nested loop until `required` and `having` variables are equal. If the current substring's length is less than minimum substring's length which is calculated so far, just update `minLength` variable with current substring length and also the substring boundaries. Thereafter, shrink (or remove the left character) the substring window and decrement the character count in first hashmap. This logic is required to find the minimum possible substring window in the first string. If the current left character found in second hashmap and first hashmap character count is less than second one, `having` variable needs to be decremented. |
23 | 42 |
|
24 | 43 | 11. Repeat steps 9-10 until the end of the first string.
|
25 | 44 |
|
26 |
| -7. If the leftIndex and rightIndex are still equals to -1, return empty string. Otherwise the substring based on leftIdex and rightIndex should be returned from first string. |
| 45 | +12. Return an empty string if the minimum substring length is not a maximum value. Otherwise return the substring of first string based on substring boundary index values. |
27 | 46 |
|
28 | 47 |
|
29 | 48 | **Time and Space complexity:**
|
30 |
| -This algorithm has a time complexity of `O(m) + O(n)`, where m and n represents lenghts of first and second strings. This is because we are traversing both the strings. Also, it requires space complexity of `O(m) + O(n)` due to two hashmaps used to store character count. |
| 49 | +This algorithm has a time complexity of `O(m) + O(n)`, where `m` and `n` represents lengths of first and second strings. This is because we are traversing both the strings. |
| 50 | + |
| 51 | +Also, it requires space complexity of `O(m) + O(n)` due to two hashmaps used to store the character count. |
0 commit comments