Minimum Window Substring in DSA C - Time & Space Complexity
We want to understand how the time needed to find the smallest substring containing all characters of another string grows as the input size increases.
How does the algorithm's work change when the input strings get longer?
Analyze the time complexity of the following code snippet.
int minWindow(char* s, char* t) {
int need[128] = {0}, window[128] = {0};
int left = 0, right = 0, valid = 0;
int start = 0, len = 0x3f3f3f3f;
for (int i = 0; t[i]; i++) need[(unsigned char)t[i]]++;
int total = 0;
for (int i = 0; i < 128; i++) {
if (need[i] > 0) total++;
}
while (s[right]) {
char c = s[right];
right++;
if (need[(unsigned char)c] > 0) {
window[(unsigned char)c]++;
if (window[(unsigned char)c] == need[(unsigned char)c]) valid++;
}
while (valid == total) {
if (right - left < len) {
start = left;
len = right - left;
}
char d = s[left];
left++;
if (need[(unsigned char)d] > 0) {
if (window[(unsigned char)d] == need[(unsigned char)d]) valid--;
window[(unsigned char)d]--;
}
}
}
return len == 0x3f3f3f3f ? 0 : len;
}
This code finds the smallest substring in s that contains all characters of t using a sliding window approach.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The outer while loop moves the right pointer through string
s, and the inner while loop moves the left pointer to shrink the window. - How many times: Each character in
sis visited at most twice, once when expanding the window and once when shrinking it.
As the input string s grows, the number of operations grows roughly in proportion to its length because each character is processed a limited number of times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 (each char visited twice) |
| 100 | About 200 |
| 1000 | About 2000 |
Pattern observation: The operations grow linearly with input size, doubling roughly when input size doubles.
Time Complexity: O(n + m)
This means the time grows linearly with the size of both input strings, processing each character a limited number of times.
[X] Wrong: "The nested while loops make this algorithm quadratic (O(n²)) because of the inner loop inside the outer loop."
[OK] Correct: Each character is visited at most twice, so the inner loop does not run fully for every outer loop iteration, keeping the total operations linear.
Understanding this sliding window technique and its linear time complexity helps you solve many substring and array problems efficiently, a valuable skill in coding interviews.
"What if we changed the input to allow Unicode characters beyond ASCII? How would the time complexity change?"
Practice
Minimum Window Substring problem?Solution
Step 1: Understand the problem statement
The problem asks to find the smallest part of a string that includes all characters from another string.Step 2: Compare options with the problem goal
Only Find the smallest substring in a string that contains all characters of another string matches the goal of finding the smallest substring containing all characters of another string.Final Answer:
Find the smallest substring in a string that contains all characters of another string -> Option AQuick Check:
Minimum Window Substring = Smallest substring containing all chars [OK]
- Confusing with longest substring problems
- Thinking it counts substrings instead of finding minimum
- Mixing sorting with substring search
Solution
Step 1: Check correct array initialization syntax in C
int freq[256] = {0}; uses correct syntax to initialize all 256 elements to zero using brace initialization.Step 2: Analyze other options for errors
int freq[26]; for(int i=0; i<26; i++) freq[i] = -1; does not initialize all elements and sets to -1 incorrectly. int freq[128]; memset(freq, 1, sizeof(freq)); uses memset with 1 which sets bytes to 1, not zero. int freq[256]; freq = {0}; tries to assign array which is invalid in C.Final Answer:
int freq[256] = {0}; -> Option AQuick Check:
Correct zero initialization = int freq[256] = {0}; [OK]
- Using memset with wrong value
- Trying to assign arrays directly
- Not initializing all elements
char s[] = "ADOBECODEBANC";
char t[] = "ABC";
// Assume function minWindow returns the minimum window substring
char* result = minWindow(s, t);
printf("%s", result);Assuming
minWindow is correctly implemented, what is printed?Solution
Step 1: Understand the input strings
String s is "ADOBECODEBANC" and t is "ABC". We want the smallest substring of s containing A, B, and C.Step 2: Identify the minimum window substring
The substring "BANC" is the smallest substring containing all characters A, B, and C.Final Answer:
BANC -> Option CQuick Check:
Minimum window substring for "ABC" in s = "BANC" [OK]
- Choosing first substring containing all chars instead of minimum
- Confusing order of characters
- Ignoring repeated characters
int left = 0, right = 0;
int count = 0;
while (right < strlen(s)) {
if (freq[s[right]] > 0) count++;
freq[s[right]]--;
right++;
while (count == t_len) {
if (right - left < min_len) {
min_len = right - left;
start = left;
}
freq[s[left]]++;
if (freq[s[left]] > 0) count--;
left++;
}
}
// Returns substring from start with length min_len
What is the main bug in this code?
Solution
Step 1: Analyze freq array usage
The freq array is decremented and incremented but not initialized, so it contains garbage values causing incorrect logic.Step 2: Check other options
Using strlen(s) inside the loop condition causes repeated length calculation is inefficient but not a bug. Incorrect update of count when moving left pointer logic is correct for count update. The inner while condition should be count >= t_len is incorrect because count == t_len is the correct condition.Final Answer:
freq array is not initialized before use -> Option DQuick Check:
freq must be zero-initialized before use [OK]
- Ignoring initialization of freq array
- Misunderstanding count update logic
- Confusing loop conditions
s = "aabbccdd" that contains all characters of t = "abc". Which approach correctly handles repeated characters in s and t to find the minimum window?Solution
Step 1: Understand the need for frequency tracking
Sincetcan have repeated characters, we must track how many times each character is needed and how many times it appears in the current window.Step 2: Use two frequency arrays and sliding window
One array stores counts of characters int, another tracks counts in the current window. Expand right pointer to include chars, contract left pointer to minimize window while keeping all required chars.Final Answer:
Use two frequency arrays: one for t counts and one for current window counts, then expand and contract window accordingly -> Option BQuick Check:
Two freq arrays + sliding window = correct approach [OK]
- Ignoring repeated characters in t
- Using only unique character counts
- Stopping window expansion too early
Ready to prove it?
Pass the test and this pattern moves unknown
