Mark As Completed Discussion

A Brute Force Approach

Given this problem, how might you solve it in a naive way without using a sliding window? Well, like I mentioned before with the brute force approach, you could scan through the entire string “HGFDSAXZBJKC” to test against all substrings of length 4. This is because “ABKC”, our "dictionary" of sorts, is 4 letters long, and takes into account the case where a substring consisting of "ABKC" is immediately found.

Provided you found all 4-letter substrings, you would then need to check each of them to figure out if they have all the characters that you need.

If you are unable to find all of required characters in the substrings of length 4 (meaning the minimum is longer than 4), we would then have to try alternate substrings of other lengths. This means we would then seek to look at substrings of greater lengths (5, 6, 7, and so on)-- with each possible length requiring a new iteration of the entire array.

A Brute Force Approach

Again, this can be extremely time consuming as it has a time complexity of O(N²). We're also missing out on information regarding how close we are to the ideal substring-- as we'll find out later, a sliding window can provide you with knowledge about how close we are to the desired range. We ascertain this from the location of the two pointers.