Minimum Window Substring


10 Prerequisite LeetCode Problems
“Minimum Window Substring” is a classic problem about using two pointers and a sliding window technique to solve substring problems. Here are 10 problems to prepare for it:
“Two Sum” (LeetCode Problem #1): This is a simple problem using two pointers, which can help you understand the basic idea of two pointer method.
“Reverse String” (LeetCode Problem #344): This problem will give you an understanding on how to manipulate two pointers on a string.
“Maximum Subarray” (LeetCode Problem #53): This problem helps to understand the sliding window concept, though the window size isn’t variable in this case.
“Subarray Sum Equals K” (LeetCode Problem #560): This problem is a step up from Maximum Subarray, as it introduces the concept of variable window size.
“Find All Anagrams in a String” (LeetCode Problem #438): This problem is similar to Minimum Window Substring but simpler. It’s a good practice for using sliding window to check all substrings.
“Longest Substring Without Repeating Characters” (LeetCode Problem #3): It is a fundamental problem to understand sliding window technique on strings.
“Sliding Window Maximum” (LeetCode Problem #239): This problem involves a slightly more complex sliding window problem, where you have to keep track of the maximum in each window.
“Permutation in String” (LeetCode Problem #567): This problem is similar to the Minimum Window Substring problem but is easier as it asks for a permutation (anagram), not a subset.
“Longest Repeating Character Replacement” (LeetCode Problem #424): This problem introduces another complexity to the sliding window technique where you’re allowed to replace characters.
“Fruit Into Baskets” (LeetCode Problem #904): This problem makes a good practice to understand the concept of maintaining a sliding window with certain restrictions.


Problem Classification
This problem requires finding the smallest window in a string containing all characters of another string. It involves analyzing substrings and their properties, hence Text Analysis / Substring Properties.
This problem asks for the smallest part in a text that contains all characters of another text. It’s about analyzing parts of a text and their properties, thus Text Analysis.
Language Agnostic Coding Drills
Let’s break down this code into smallest units of learning. The code is a solution to finding the minimum window in string s
which contains all characters of string t
.
Understanding Basic Programming Concepts Understanding variables, basic data types (int, float, string), conditional statements and loops.
Understanding Classes Understanding the basics of classes, methods and the
self
keyword in Python.Working with Strings Understanding how to manipulate and traverse strings in Python.
Understanding collections.defaultdict Using defaultdict to handle missing keys in a dictionary.
Understanding Enumerate Understanding the use of
enumerate()
for getting index/value pairs in a loop.Manipulating Dictionary Understanding how to add, remove, check elements in dictionary and how to use it to keep count of characters.
Understanding if Conditions and Loops Understanding and using nested if conditions and loops in Python.
Manipulating Lists Understanding list indexing, slicing and methods to add, remove elements from list.
Understanding the Sliding Window Concept Using two pointers to create a window and adjust its size.
Understanding and Implementing the Solution to the Problem Combining all of the above to understand and implement the solution to the problem.
Starting with basic programming concepts like variables, loops, and if conditions, we gradually move up in complexity to understanding classes, strings, dictionaries and then to more complex concepts like using collections.defaultdict
, enumerate()
, and understanding the sliding window concept in algorithms. Finally, we combine all these to understand and implement the solution to the problem.
Targeted Drills in Python
Let’s create some drills based on the concepts identified:
Understanding Basic Programming Concepts Goal: Create a program that uses variables, basic data types, conditional statements, and loops.
1 2 3 4 5
number = 10 if number < 15: print("Number is less than 15.") for i in range(5): print(i)
Understanding Classes Goal: Define a simple class with a method.
1 2 3 4 5
class HelloWorld: def say_hello(self): print("Hello, world!") hello_world = HelloWorld() hello_world.say_hello()
Working with Strings Goal: Manipulate and traverse a string.
1 2 3 4
s = "Hello, world!" print(s.lower()) for char in s: print(char)
Understanding collections.defaultdict Goal: Use defaultdict to handle missing keys in a dictionary.
1 2 3
from collections import defaultdict d = defaultdict(int) print(d["missing_key"]) # prints 0, the default value
Understanding Enumerate Goal: Use enumerate to get index/value pairs in a loop.
1 2 3
lst = ["a", "b", "c"] for index, value in enumerate(lst): print(index, value)
Manipulating Dictionary Goal: Manipulate a dictionary to count the frequency of characters in a string.
1 2 3 4 5 6 7 8
s = "hello" freq_dict = {} for char in s: if char in freq_dict: freq_dict[char] += 1 else: freq_dict[char] = 1 print(freq_dict)
Understanding if Conditions and Loops Goal: Use nested if conditions and loops to find all even numbers in a 2D array.
1 2 3 4 5
arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] for sub_arr in arr: for num in sub_arr: if num % 2 == 0: print(num)
Manipulating Lists Goal: Use list indexing, slicing, and methods to manipulate a list.
1 2 3 4 5
lst = [1, 2, 3, 4, 5] lst.append(6) print(lst[0]) # prints 1 print(lst[1]) # prints 6 print(lst[1:4]) # prints [2, 3, 4]
Understanding the Sliding Window Concept Goal: Use the sliding window concept to find the maximum sum of any contiguous subarray of size
k
.1 2 3 4 5 6 7 8 9 10 11 12 13 14
def max_sum_subarray_size_k(arr, k): max_sum = float('inf') window_sum = 0 window_start = 0 for window_end in range(len(arr)): window_sum += arr[window_end] if window_end >= k1: max_sum = max(max_sum, window_sum) window_sum = arr[window_start] window_start += 1 return max_sum print(max_sum_subarray_size_k([2, 1, 5, 1, 3, 2], 3))
Understanding and Implementing the Solution to the Problem The goal for this drill would be to understand and implement the solution to the minimum window substring problem using the