76. Minimum Window Substring

note : this first problem has been brutally analyzed as this is a tricky concept and forms the basis to solve the next 5. Master this one and try the rest on your own, that would be the best way to learn.

In the previous example the window was of a fixed size, but here we use a window of variable size determined by begin and end markers. A brute force approach would be to iterate through all possible substrings and determine the minimum window which contains all characters of T. How do you see if a substring has all characters of T ? You could use the frequency table of T which stores character to number of occurrences as follows :

# initialize frequency table

for char c in T do

table[c]++;

end counter = table.size() # unique chars in T for char c in string B do

if(char c in table){

table[c]--; # decrement count for c

if(table[c] == 0) counter--;

}

end if(counter == 0){ # B has every character in T }

So we are basically assuring that every unique character in T exists in B as many times as it exists in T by maintaining a counter. It is fine if there are 4 ‘K’s in T and B has 7 ‘K’s , the table count for ‘K’ would simply go negative, but it goes to 0 at some point before that, proving string B has at least 4 'K’s in it, satisfying the need with respect to “K”, extending the logic to other chars in T if counter = 0, B has all chars in T.

Okay so coming to the sliding window part here, we keep sliding end to the right examining each new character and updating the count in table. As soon as we hit counter = 0, it means we have a valid answer so we try to trim it down removing the unessential characters from the start by sliding begin right. We constantly keep trying to validate / invalidate the string by manipulating counter and table counts.

Take a moment, understand this code. Walk through it on paper for this example : [ S : ADOBECODEBANC | T : "ABC" ] . Do not worry, the code is just heavily annotated, it is actually very concise.

Intuition : the best substring for the answer would simply be a permutation of T if such a substring exists in S, but otherwise we could have wasteful characters sitting in between the essential characters that make the substring valid as an answer. Our attempt here is to remove such chars without losing the necessary ones. After trimming down as much as possible we resume with sliding end right and repeating the whole process.

Whenever counter = 0 we have a valid candidate for our ans, but we update ans only if it is shorter than previously recorded minimum length ans.

Exactly the same as above with the added condition that the substring should be of length equal to p and that we have to return indexes of all such occurrences.

Here comes my proof that not all Leetcode “hard” are exactly hard, this one is simply a slight modification of the above problem which is tagged “easy”. Instead of chars in above question now we have words so it got a bit messier.

Here we build the frequency table out of the substring we explored so far as the aim is to find longest susbstring without any repetitions. So similar to above case we slide end and keep building the table and as soon as we hit a repetition, meaning the end char is already in table, we start sliding begin. We record the max before sliding begin for the ans.

Other problems that can be solved similarly:

340. Longest Substring with At Most K Distinct Characters which is literally the same as the one with K = 2.

424. Longest Repeating Character Replacement

A good exercise at this point would be to think why the sliding window approach actually works, draw out all the cases possible for the minimum window substring problem and you’ll have a deeper intuition into this technique.

Some interesting facts :

It took me less time to actually solve these problems than what it took me to figure out how to explain it in writing. I solved a problem tagged hard first and the corresponding easy problem later as I use an extension to hide the difficulty levels on leetcode loll. I strongly believe understanding how a programmer wrote code is more important than the code itself and so I am working on codeback , a code playback tool allowing users to stop, edit and play the code line by line as well as execute the code to see output.

Stay tuned and feel free to connect with me on LI : https://www.linkedin.com/in/sourabh-reddy