 What's up guys, Mike Dakota here. Today we are going to go over the sliding window algorithm and we're going to see how we could apply the sliding window algorithm into many different types of problems. So then if you could see this similar problem of the sliding window, you could apply this algorithm to those problems. This video is not going to go over each individual problem in depth. We're just going to explain the sliding window algorithm in each of these situations and how you could apply them to each of these problems. So what is the sliding window algorithm? The sliding window algorithm is used to find problems involving contiguous sub arrays or sub lists. Okay, so as you can see in this picture right now, we have a sliding window. What does this mean? It means that we have basically this window that we're going to add values into it or we could remove values after it. Okay, so we have 11512, right? If I move this window forward, this window now shifts to the right and we have 15126. So essentially it was what we're doing. What we did here is that we removed the one and we added a six into this window. So this is what a sliding window is doing. We're taking, we have a window here and we're just moving this window either forward or backward down our array. So what is the sliding window? When do we use a sliding? Many problems to deal with sliding window is that we need to calculate something or find something out of contiguous sub arrays of a given size. So what do we mean by contiguous sub array? Well, a sub array is a small array part of a bigger array. So in this big array of 1326-14182, a sub array would just be picking any elements. So if I pick like a four, and then I pick two, right? And I put that in a new array, four, two, that would be a sub array, a new array that is part of the small array part of the bigger array of 1326-14182. But what they want is contiguous sub arrays. So what does contiguous sub array means? It means I can only take one after the other. Okay, so that means that negative 1418, this sub array, negative 1418 is contiguous. Okay, that means I can only pick elements one after the other. I cannot just take negative 1418 and then pick a one here also from and merge them together as a new array of one negative 1418. Okay, that's not allowed. Okay, we cannot do that. We can't just pick elements randomly and have some space between they have to be one after the other. So that's what negative 1418 is a contiguous sub array. Okay, so now in this problem, given an array, we need to find the average of all contiguous sub arrays of size K. So what does that mean? So let's say K is equal to five. What essentially what we need is we're trying to find the average of all contiguous sub arrays of size five. So what does that mean? That means that we're just going to go through all the elements in this array. And we're going to go to each contiguous sub array of length five. So if I start from one, and because they have to be one after the other, because they're contiguous, I have to just count five elements including one. So here I have one, and then I have to count five elements. So I have one, two, three, four, five. So I'm going to add that negative one. So this would be a contiguous sub array, right? One, three, two, six, negative one. And this is a contiguous sub array of length five. So because they want the average of all contiguous sub arrays of length five, what we're going to do is we're going to add them up. Okay, we're going to add up, we're going to sum up all these elements of one plus three plus two plus six plus negative one. And we're going to divide by five, because that's the average, right, the length is equal to five. And the average is you is the sum of all the elements and divided by the length of it. Right. So then in this case, we have the first contiguous sub array of the average is going to be one plus three plus two plus six, plus negative one divided by five, we're going to get 2.2. Okay. So that's the first contiguous sub array. Now we want to go through there all of them, right? So now we have to go to the second one. So what's the second one more? Well, starting from the second element, we're going to count a length five. So starting the second element right here, we're going to count five elements of it. So one, two, three, four, five. So this would be the second contiguous element of length five, right? Three, two, six, negative one, four. Right. This would be the second contiguous sub array of length five. So then what do we do now? We're going to sum them up and divided by five. So what they do here is that they do three plus two plus six plus negative one plus four divided by five, they get 2.8. Okay. So that's the next element, right? And then now we're going to go through the next contiguous sub array. So here, this would be the next contiguous sub array, the third element starting from the third element, and then length five. So third element is two, and then we're going to count five elements of it. So we're going to do two, six, negative one, four, one, right? These are five elements starting from two. And what we're going to do is we're going to add them up. Okay, so we're going to add them up. So we're going to two plus six plus negative one plus four plus one. And I think that's, that's 2.4. Okay. And then we're going to add up the next one here, six plus one negative one plus four plus one plus eight, and then we divided by five, and then we get some, I don't know, 2.4, whatever. Yeah, what it is, what is okay. And then after that, we got to add up the last one, negative one, four, one, two, sum them up divided by five. And that's our last value of 2.8. Okay. So that's how you would calculate the sums individually. And just starting from each of the indexes, and then go looping through, and then we're going to go through every single five elements, starting from each of the indexes, and then adding them up and dividing by five. Okay. So what would the algorithm look like? Well, what it's doing is it's going to go through the number of sub arrays inside the, the array, and then they're going to go through every length of five, right? So the, so their first loop is going to start from zero, it's going to go through n minus k, right, that's going to be the last ending, the last spot that they're going to end at. And their second loop is just going to sum up each of the individual sub arrays. So that's going to start J is going to equal to I, which is like where they started from the beginning, and they're just going to loop up to I plus K, which that means is that they're going to loop through every five elements, okay, they're going to loop through every five elements and just continue going so on and so forth. So the sum is going to start from zero, and it's going to go from R of J, and it's going to just keep adding up for every single five elements, and then they're going to calculate the average and divide by K. And they just store that into like the next index in their resulting array, and they return it. So this is just going to go through every single length of five of a sub array of five, and then just sum up, sum them up, get the average, and then put that into a new array. And this is just takes all time complex to n times k, because there's two loops, and then the size of it would be k, right? So there's k, there's k size and you got to multiply by n, which is n times k. So what's the problem with this solution? Well, the problem is that we're recalculating overlapping parts. So in the first sub array, we have one, three, two, six, negative one, right? And we sum them up. Well, our second sub array, three, two, six, negative one, four, is recalculating the sum of three, two, six, negative one, right? They're overlapping parts that are actually overlapping that we're recalculating, which is not actually what we want to do. So the most efficient way to solve this problem is actually just to have a window of five elements, and then just move, reuse the sum from the previous sub array. So we're going to subtract one element out of the window. And then we're going to add a new element from the right side into our window. So it saves us from having to recompute this previous sum over and over again. So the time complexity gets reduced to o of n. So in this, in this sub array, we can take a sub array, we have one, three, two, six, negative one, we're going to remove, subtract this one from the beginning of our sub of our sliding window, and we're going to add four next element of four into our sliding window. So then we're going to still have five elements here. And we already have the previous sum that we've calculated. So we don't have to recompute that over and over again. So how do you code this problem? Very easy. Okay, all these solutions are in Java, but you could do the exact same thing in JavaScript in C++ and C in Python. The same concept applies to all these languages. Okay. So how do you code this? We're going to have the ending of our window, we're going to loop from zero to the length of the array. Okay. And what we're going to do is we're going to have a variable called window sum, it's going to add up all of the out of the current elements, all right, of our window end. Okay, so we're just going to sum up the values in in window sum. Okay. Now, if we reach size K, okay, so if this if statement is just checking if we if window plus one is greater than equal to K, right, or a window end is greater than equal to K minus one, this is just checking if we've reached K values already. Okay. So if we've reached the size of K values, what we're going to do is we're going to calculate the average. So we're going to store the result of our array, this result is an array, right, we're going to store the results of a window start, which is just going to be another variable equal to the average, okay, the average is going to be a window sum, which is our current sum divided by K. Then what we're going to do is we're going to move the window down. And to do that, we're going to subtract the current, the first element out of the R window start, okay, so it's going to be going to remove whatever element it was at the index of window start, right? So we're going to remove the element of whatever was at the window start out of our sum. So we're going to subtract it out of our window sum. Then what we're going to do is we're going to shift the index of window sum, window start down, down by one. So it's going to essentially slide the window down ahead. So if what it's doing is it's removing the first element, and they're going to move the next index downwards, then we're going to go to the next elements, and then it's going to continue on so on and so forth, as the window start, as window sum goes to window end, and we reach the end of our array. So that what's that doing is that's going to maintain the window loop through, maintain the window of five elements, remove the first element, add the next element, move the first the next element, have the next element, so on and so forth, maintaining five elements in our current sum. And every time we have five elements, we're going to calculate the average, and then we put in our array of result, and then we return it. Okay, so that's what this whole algorithm is doing. All right, let's go over another problem. Maximum sum sub array of size k. So given an array of positive integers, and a number k, find the maximum sum of any contiguous sub array of size k. So what is this, it's basically the exact same problem. But instead of finding average, we're going to try to find the maximum sum. So let's just actually look at the code right now. If you look at the code, it's literally very, very similar, exactly the same thing. Or instead of having an average or a current sum, we have max sum, we have window sum, we still have the same window sum, right, we we have still have the same window sum, we have the window end, starting from zero, going up to the length of r dot length, we still have window start, equal to zero. And then we're still summing up window sum at each of the current elements currently. And we still have this if statement, if we reach a length size of k, right, if the window end is greater than equal to k minus one, this is just pre checking if we reached the size of k array. Now the only difference is that we have is that now, instead of taking the average, we're going to get the max, right, so we're going to get max. And we're just going to have the max of the, we're going to just try to get the max sum. So to do that, we call max function and pass in our max sum, and the current window sum that we're passing in, going through and summing. Now here, we're still going to remove the first element from window sum, as we go down our array, and we're going to still increase the window start, which is the index of window start of whatever windows are starting to the next element, and then we maintain five elements going downward. But this time we're trying to find the maximum sum. Okay, and then we return maximum. So this is pretty much the exact same very similar code. Only difference is that this time they're asking is maximum sum of any contiguous sub array size k. So instead of maximum sum, instead of average, they're asking maximum sum, okay, of contiguous sub array. Okay, so once you see this, you should realize this is not that very difficult. Alright, so next let's go over the next type of problem. This time, smallest sub array with a given sum. So given an array of positive numbers in a positive number s, this time, find the length of the smallest contiguous sub array whose sum is greater than or equal to s. So this is very, very pretty much very similar to the previous problem, except instead of having a fixed window length of k, this time we're given the sum s and we need to find the smallest length. We need to find the smallest length of the smallest contiguous sub array, the length of the smallest contiguous sub array, whose sum is just greater than or equal to s. Okay, so let's just go directly to the solution now, because it's really not that hard. So since we don't have a window anymore, like we don't have like a fixed length, we're just going to loop from zero. We're just going to assume we're going to loop from zero to the end of the array, right, and that's going to be our like a window size. Okay, so this is just going to be like, like the size of our window, right? Our window is going to shrink or increase or shrink. Okay, what we're going to do is we're just going to sum up the elements in this current window size. So our current window size is going to be zero, and then it's going to go to the length of the array, right? Now, what we're going to do is while this sum, this window sum is greater than or equal to s, right? So this is very similar to the whole sliding window algorithm. But this time, while the window sum is greater than or equal to s, so this is like the s is like the sum that they give us now. So this time s, they give us the sum, but we need to find the smallest length, right? So while this window sum is greater than or equal to the s, what we're going to do is we're going to calculate the minimum length. And to do that, we have this variable called min length. And we set this equal to some like infinite large value, right? And we're going to calculate the length of it, the length of our window by taking the window length, window end, which is another variable, right? Window end minus window start, which is going to be zero in the beginning, right? I think window end is, did they define it here? I think they defined it, defined it somewhere here. I think they, I don't know. But yeah, so take window end minus window start and then add one so that basically counts for, wait, I think, I think this should have been window size. They didn't define a window end. So this is like, this is a problem. This is like a typo. So I think this should be window size. But essentially is that what they're doing is that they're going to have a fixed starting from zero and go to the length n, and they're going to sum up continuous sum up of the current values in the array. And then while this sum is greater than equal to s, they're just going to calculate the length by taking the window of the window end minus the start and the plus one, which is going to give us the length of each of the array. And then what they're going to set that equal to the smallest min length, equal to our variable of min length, to calculate it, use a min function, and set that set set this length is going to the minimum value of this giant length is going to equal with the minimum current min length, get the smallest value between both these two values, calculate it put in min length. And then we're going to reduce our window size. So what we're going to do is we're going to subtract the windows window start, which is going to be the starting value of our starting index, we're going to move it up by one. And then we're going to yeah, we're going to remove that from our window, the first element we move that from our window, and then we're going to move increase our window start plus plus. So instead of having a fixed length, this time, we are just looping through zero to total length, and then summing them up. And then while it's greater than equal to s, we're just going to subtract it, right, subtract the star window start, like a subtract our window length, over and over again, and move the window downwards. Okay, so that's what this is doing. In the end, if we min length is equal to still equal to infinity, then it's zero. Otherwise, it's we're just going to return min length. So yeah, that's essentially how you do this problem of smallest sub array with a given sum. Instead of having a fixed length of k, we're trying to find the smallest k smallest length, let's go over the next problem. Given a string find the length of the longest substring in it with no more than k distinct characters. So this is very, very, very similar. Okay, so when they mean that no more than k distinct characters, they mean that we don't want more than k distinct characters, we want the length of the longest substring in it with no more than k distinct characters. So how do you do this problem? Let's just go to the solution right now. We're going to our window now is going to be a hash map storing the frequency of each character. Okay, so that's what they have here. And then what they're going to do is that once we we're going to keep storing the frequency each character, and we're going to have a, a index of the start window start and the index of the window end. And what we're doing is that we're going to keep adding more values into our hash map, right? And what we're doing is that if we reach k, so they don't want they want they don't want more than k distinct characters, right? So if our size of our map is greater than k, we're going to move the window to the to the right. Okay, and we're going to decrease the frequency of the leftmost character. So that's what they do here. So they get the leftmost character here. And then they're going to decrease the frequency of it. So that's what this put thing is doing is decreasing the frequency of the leftmost character. And then if if it is equal to zero, right, so if the frequency reached like zero or negative or whatever it is, they were just completely remove it out of out of the out of the map, because like, what's the point of it, right? And then they're going to increase window start, which is they're just going to move the window downwards, right? So instead of having like our window being an array, our window is now a hash map. And we're just moving the window downwards for the number of frequencies so that make sure that our size is not number distinct characters is not not greater than k, right? It has to be no more than k distinct characters. So that's what they do here. So that's what this sliding window is doing down here. And then they're just going to get the maximum length, getting max length minus window start plus one, which is going to get the length of the window. And then they store that in a variable called max length, and they just return it. So yeah, that's this part here. So yeah, what they're doing is very, very similar. Put a store in a hash map, add one to the right every single time. Once we reach k distinct characters, move the window from the start, remove the first element of the characters in the first window on the left side of the window. If it reaches zero, we remove it from our map window. And then we're just going to move the window start down by one. And then we're going to calculate the length of our window and the star window start and window end. Okay. So yeah, that's the gist of this problem. And let's go to the next. Okay, next problem 1.4 fruits into a basket, given an array of characters where each character represents a fruit tree, you're given two baskets. And your goal is to put the maximum number of fruits in each basket. The restriction is that each basket can only have one type of fruit. You can start with any tree. But once you have started, you can't skip a tree. You will pick one fruit from each tree until you cannot pick it. And you will stop when you have picked from a third fruit type, write a function that returns the maximum number of fruits in both of the baskets. Okay, let's just go directly to the solution. It's very, very similar, except essentially is that we need to find length of the long sub array with no more than two distinct characters, which is the fruit types. So it's very similar to the last problem. Have a hash map storing the frequency of each character, loop through from our starting window to the end, increase the frequency of it, while the free the number of distinct characters is greater than two, going to decrease the frequency of the starting value of our window, if it equals zero, remove it from our window, then we're going to increase the window start, which is the starting index plus one. Then in the end, we get our, we calculate our length of our maximum fruits by taking window and minus window start plus one, so that calculates the length, and then we return it. So it's very similar to the previous question. Okay, so next problem given a string find the length of the longest substring that has no repeating characters. Okay, it's very similar. We're going to have a hash map that stores the each character, and then this time it's going to store the index of each of the last character, okay, of the last character that we go through. So we're going to maintain our window going from zero to the end of our string, and we're going to get the last character at the current window. And what we're going to do is that if we already contained this right most character, right, if we already can process the string, have a repeating character, what we're going to do is we're going to move the starting window down by one, right, move it down. And then essentially is that whenever we get a repeating character, we will slide our window to ensure that we have distinct characters in the sliding window. Okay, so we're going to, yeah, we're going to get the maximum start window start and then the right most character plus one, where we'll start down. And then we're going to put the right most character, right character, our window and put that that frequency there, like not frequency that that index there. And then we're going to just get the recalculate the length of window length minus window star plus one, which, excuse me, which gets our window max length. And then we return it. So yeah, it's very similar. But this time, the hash map, our window stores the last index of each character. And if we get a repeating character, so if it contains this character, we shrink our window to make sure that we all have distinct characters. Alright guys, this is the next problem of sliding window, the longest subarray with same characters after replacement. So you're given a string s in integer k, you can choose any character of the string and change it to any other uppercase English character. So basically, we have a string and we want to change this character. We could change this character to an uppercase at most k times. And after this, we need to return the length of the longest substring containing the same letter you get after performing the above operations. So if we just go into the solution here, it's pretty much the exact same thing. So instead, we're going to have a frequency array, and this is going to be our window per se. And then we're going to have the frequency care letter, we have the left index representing the left side of our window. And then we have max the max length, okay, max length of the letters to replace. So what it's doing here is it's going to go from the right window to the end, sorry, from zero to the length of the string, we're going to mean to increase the frequency of each character at each certain number, right, each character. So character A would have a frequency, we're going to plus one character B would have another frequency plus two. And then what we're going to do is we're going to get the most frequent character, maintain this as a variable, and using the frequency array, maintain most frequent character. And what we're going to do is we're going to take the window that we're having so right minus left plus one. So this is like the length of the window. And then we're going to subtract by the most frequent character to get the number of letters to change. So because we're changing a certain number of letters, right, to become uppercase, right, at most k times, we need to get the amount of times that we're changing it. Right. So we're taking the length of the string that we're going through, like the length of our window subtracting by the most frequent character, the frequency of the most frequent character, right, and that's going to give us the letters that we're changing. And if it's greater than k, then that means that we have to move our window up. So what we're doing is we're going to decrease the frequency of the starting left side of the starting window, and increase it, go to the right side, right, the left one is going to go to the right, and then we're going to get the max of our window again. And then after that, we just return max. So this problem is very similar to the previous problem. Next problem is very similar, 1.7, which is the longest subarray with ones after replacement. This time we're given an array of ones and zeros and a number k. And we need to return the maximum number of consecutive ones that we could flip at most k times to get zeros. Okay. So if we look at the solutions very similar, this time we're going to have same variable max, zero count, which is going to represent like a counter for the current window. Then we're going to have our left pointer, which is like the left, the left side, i is going to represent the left boundaries of our window. And j is going to represent the right boundary of our window. So we're moving j from zero and going to the length of n. So if the number is zero, because in this problem, we're given ones and zeros and we want to flip them to most k zeros, right? So if it's equal to zero, we're going to increase the count of zero. And then while the count is greater than k, what we're going to do is we're going to move the left window to the right. So here we're decreasing the count of the zero count on the left side, and then we're going to increase the left pointer of our window to the right, right? We're going to increase the left pointer to the right. And then once this is done, we are going to get the maximum length of our window. And that's, and then we're going to return that. So it's very, very similar to the previous problem. Only difference is that instead of zeros, it's like, we're trying to flip zeros and ones instead of like strings. But yeah, that's very, very similar. Next problem we're going to go over is given two strings s one and s two return true if s two contains a permutation of s one. So in this case, we have a b. And here we have e i b e i d b a o o o. So since s two contains a permutation of a b, such as b a, that's going to be the output. It's going to return true. Yeah, whereas here, a b, there's no b a value here, there's a space, there's an o in between. So this returns false. Okay, so that's like the inputs. So in this problem, the easiest way is to create a frequency array for both of them, add them up. And then if one of the both of the arrays are have the same number of frequencies currently, while you're looping through them return true. Otherwise, we're going to decrease the frequency, increase the frequency of the rightmost, and then decrease the value of the current character, to the rightmost also. So we're basically just decreasing increasing and decreasing it while we're looping through the difference of the two lengths of the two strings. Okay. So yeah, now this is pretty difficult. This can actually get further optimized because instead of updating it, we could actually keep a counter. So here we're going to have the same sliding window now. So to do so, we're going to maintain a counter variable which stores the number of characters, which have the same frequency of s one and s two. And as we slide the window down, we deduct the last element and added a new element to a new frequency of characters, increasing it by one. Otherwise, you keep the count intact. Okay. If it was earlier added, it now leads to a frequency mismatch. So we need to decrement the count variable. So we have to shift. So after shifting the window, if the county equals to 26, that means all of them match. So in this case, we have the same frequency array for s one string and s two, we're going to keep counter that this time we're going to have a count variable. Then we're going to loop through. Yeah, we're going to loop through a counter counting how many characters are the same in both of the frequency arrays from the first one and the second frequency, and we increase it. Now this is where we have our sliding window. So here, what we're doing is we're going to start from zero and we're going to go through s two dot length minus s one. And then we Yeah, so here, this is our rightmost window, R. And this is just like a frequency of it. And this is the leftmost. And this is like the character number of the frequency. No, not frequency. My bad. It wasn't frequency. This is like the right index. And then it's like the left index. Okay. What we're doing is that if the count is equal to 26, that means that we already have all the characters of the same. Since there's 26, there's 26 letters, right? So then what we do is we're going to return true. Otherwise, we're going to increase our frequency on our rightmost pointer. Okay. And then if the rightmost is equal to the left most to to the rightmost pointer is equal to the left, the first, the rightmost of the first frequency array, we're going to increase the count. Otherwise, if it's rightmost is like one less than it, right, we're going to decrease the count. And then then we're going to decrease the count of the rightmost character. And then we're what we're going to do is if they're both equal on the left most, then we're going to increase the count. Otherwise, they're not, we're going to decrease the count also, right? If the left side of the rightmost left side of the second strings frequency is equal to the left side of it, minus one, then we're going to decrease the count. And we return if our count is equal to 26, because that would mean that we have the count of frequency match being the same. Okay. I know this is kind of like just going through each of these fast. But as you can see here, it's very, very similar in that except that we're going to have a window shooting to the right. And then we're like decreasing the frequency of the right string, right strings frequency, right and the left strings. Okay. So yeah, it's very similar. Next problem is 1.9 string anagrams given two strings s and p return an array of all star indexes of p anagrams and s, you can return them in any order. The anagram is when one, the number of characters and one is equal to the same number of characters the other. So CBA is an anagram ABC because they have one C, one B, one A, and ABC has one A, one B, one C. So if we look through here, yeah, we start from zero, we see there's a CBA and that's an anagram ABC. So they add the starting index of zero into the array. And then we also see there's a BAC, which is an anagram of ABC. So they added that the starting index, which is of B, which is going to be zero one, two, three, four, five, six. So they added six into your array. Okay. So how do you do this problem? Very easy. Keep track of frequency characters of your of p, right? Second thing, keep keep frequency of that. Have a star pointer and end pointer representing the window length of the window. Our current length, we're going to set equal to just the length of the string p. This difference is going to represent the light if the length of the currently found anagram. So if it equals to the one that we find, then it's found. Okay, loop through our window, get the current character, decrease the decrement the frequency of it. Okay, then if it's greater than equal to zero, we're going to decrease our window, which is the the length of our window of the length, because they want us to find the, yeah, we have to decrease the length of the window. Okay, so we decrease the length of the window. And then after this, after the length of the length of the window zero, yeah, we're going to have like the length of the anagram, right? Okay. So if the difference is equal to zero, we're just going to add the zero index to our solution. Okay, then we're going to loop continue looping through the end of the string, get the current value, the start of the window. If it's greater than equal to zero, we're going to increase our frequency of difference, that means we're further away from the completing anagrams, we have to increment difference. Then we're going to increment the frequency for the current character, going to move our window to the right. And then we're going to get the last character of our window and decrease the frequency of it because that means that we don't need anymore, right? It's because we're decreasing it means it's part of the window now. Again, if it's not negative part of the window, so we're going to decrease our frequency, if it's greater than equal to zero, decrease frequency. If we reach zero, right, if our difference is zero, then we just add the start of our window of the start of the window of our array. And then yeah, then we increase and go to the next iteration. After that we just return solution. Okay, so yeah, that's essentially the gist of this problem. Let's go over another problem, smallest window containing substring, given two strings S and T, where you want to return the minimum window substring of S such that every character in T is inside the window. Okay, we're also including duplicates. So in this case, we have ABC, and then we the smallest window that contains ABC is bank B and C. Okay, so that's the smallest window in S that contains all the characters of ABC. Okay, so how do you do this? Create a frequency dash map, loop through it, increase the frequency of the counter of each character. We're going to have two, two pointers representing the start and then the j is going to represent the right most of the window. We're going to move j to the end. We get the current character. If the frequency array contains the frequency map contains it, we're going to decrease it. And then if it's greater than equal to zero means it's unmatched, we're going to subtract count. So count represents, yeah, count represents the number of characters that you need to match. So currently count is equal to the length of the T, which is like the second string, we need to match. So yeah, we have to subtract count, if our frequency is greater than equal to zero. Okay, because that means like, we're decreasing the number of characters that are remaining remaining. Okay. Alright, so once count is equal to zero, that means that we already found a substring, we're going to move through between our window from i and j. Set flight equals true, we get the length of our window, j minus i plus one. If the current length is less than equal to men, we're going to just set it equal to it. Okay, so yeah, it's less than equal to men, we're going to update our minimum of our left side to equal to i and minimum right side equal to j. And then we're going to get our minimum window is going to equal to the current length, because that's smaller than men. Then we need to shrink the left pointer. So to do that, we get the left character, and then we need to check if it contains it. Then, since we're shrinking it, we're going to increase the frequency count by one. And then if it's greater than equal to one, we increase count plus plus. Okay. Then here, we increment the left pointer, so i plus plus, because we're moving the window downwards. And then j, we're going to move it downwards also afterwards. So yeah, in the end, if our flight is still true, that means that we found a global minimum, we're just going to substring between the minimum left and minimum right, plus one. And then, yeah, then we just return that otherwise, there's no possible way. So we're going to return empty. Okay, so yeah, that's this gist of smallest one, our last problem. And I don't actually understand how this works. But whatever, okay, last problem will go over. You given a string s in an array of string words of the same length, and you need to return the starting indices of all substrings in s that is a concatenation of each word and words. So let's say we have this string s in the words foo bar. So substrings starting with foo and bar. So like bar foo and foo bar would be starting from index zero would be bar foo right here. And then if we want foo bar, it would be this one nine zero one two three five six seven nine foo bar. Okay, so how does this work? I'm not exactly sure. But because just try frequency of strings, okay, we have a frequency of strings, hash map, and we're just going to increment it. Now we're going to have our array of indices. We're going to have the length of our, yeah, a length of our string, length of the number of words, and then just the length of each of the work of each individual words. So we're just going to loop through n minus num, which is the words length, multiply by length of the frequency. So this is just going through, I think every single word in the frequency, I think, not a frequency, every single word in the, it's going through every single word in the array of frequency of words, I think, I believe so. Okay, and then what they're going to do is that they're going to create another hash map sink scene. And they're going to start j is equal to zero. Well, j is less than number. Number is the words length. So it's length of the words. We are just going to substring this do Hickey that I don't know what it's doing, I plus j times length. So it's going to substring each of the words. And then what's going to do is it's going to multiply by a length. Okay, s. And then if it contains it, it increases it. If the scene is greater than the get we break. Otherwise, we break. And we increment j. And if j is equal to number, we add it into our indices and then we return indices. So I actually don't know how this has to do with this. I don't actually know how this works. I'm not sure. But yeah, I don't know. Yeah, sorry, guys, I can't help you on this last problem. But yeah, I hope you guys enjoyed this video. Rick, I'll subscribe. I'll check you guys later. Peace.