Challenge - 5 Problems
Minimum Window Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediateOutput of Minimum Window Substring Function
What is the output of the following Python code that finds the minimum window substring containing all characters of t in s?
DSA Python
def min_window(s, t): from collections import Counter need = Counter(t) missing = len(t) left = start = end = 0 for right, char in enumerate(s, 1): if need[char] > 0: missing -= 1 need[char] -= 1 if missing == 0: while left < right and need[s[left]] < 0: need[s[left]] += 1 left += 1 if end == 0 or right - left < end - start: start, end = left, right need[s[left]] += 1 missing += 1 left += 1 return s[start:end] print(min_window("ADOBECODEBANC", "ABC"))
Attempts:
2 left
π‘ Hint
Focus on the smallest substring that contains all characters A, B, and C.
β Incorrect
The function finds the smallest substring in s that contains all characters of t. The substring "BANC" is the shortest that contains A, B, and C.
β Predict Output
intermediateMinimum Window Substring with Repeated Characters
What is the output of this code when s contains repeated characters and t requires multiple occurrences?
DSA Python
def min_window(s, t): from collections import Counter need = Counter(t) missing = len(t) left = start = end = 0 for right, char in enumerate(s, 1): if need[char] > 0: missing -= 1 need[char] -= 1 if missing == 0: while left < right and need[s[left]] < 0: need[s[left]] += 1 left += 1 if end == 0 or right - left < end - start: start, end = left, right need[s[left]] += 1 missing += 1 left += 1 return s[start:end] print(min_window("AAABBC", "AABC"))
Attempts:
2 left
π‘ Hint
Remember t requires two 'A's, one 'B', and one 'C'.
β Incorrect
The minimum window substring containing two 'A's, one 'B', and one 'C' is "AABBC".
π§ Debug
advancedIdentify the Error in Minimum Window Substring Code
What error will this code raise when executed?
DSA Python
def min_window(s, t): from collections import Counter need = Counter(t) missing = len(t) left = start = end = 0 for right, char in enumerate(s): if need[char] > 0: missing -= 1 need[char] -= 1 if missing == 0: while left < right and need[s[left]] < 0: need[s[left]] += 1 left += 1 if end == 0 or right - left < end - start: start, end = left, right need[s[left]] += 1 missing += 1 left += 1 return s[start:end] print(min_window("ADOBECODEBANC", "ABC"))
Attempts:
2 left
π‘ Hint
Check how the right pointer is used in slicing the string.
β Incorrect
The code uses zero-based indexing for right but slices s[start:end] where end is zero-based, missing the last character. Also, right is zero-based but the loop does not add 1, so the substring is empty.
π§ Conceptual
advancedWhy Use a Sliding Window in Minimum Window Substring?
Why is the sliding window technique suitable for solving the Minimum Window Substring problem?
Attempts:
2 left
π‘ Hint
Think about how to find the smallest substring containing all needed characters without checking every substring.
β Incorrect
Sliding window expands to include needed characters and contracts to remove unnecessary ones, efficiently finding the minimum substring.
π Application
expertMinimum Window Substring with Unicode Characters
Given the string s = "πΈπΉπΈπ»πΈπΉπ " and t = "πΈπ ", what is the minimum window substring containing all characters of t?
Attempts:
2 left
π‘ Hint
Unicode characters count as single characters; find the shortest substring containing both.
β Incorrect
The substring "πΈπΉπ " is the shortest substring containing both "πΈ" and "π " in the given string.
