0
0
DSA Pythonprogramming~20 mins

Minimum Window Substring in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Minimum Window Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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"))
A"BANC"
B"ADOBEC"
C"CODEBA"
D"DOBECODEBA"
Attempts:
2 left
πŸ’‘ Hint
Focus on the smallest substring that contains all characters A, B, and C.
❓ Predict Output
intermediate
2:00remaining
Minimum 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"))
A"AABB"
B"AAABBC"
C"AABBC"
D"ABBC"
Attempts:
2 left
πŸ’‘ Hint
Remember t requires two 'A's, one 'B', and one 'C'.
πŸ”§ Debug
advanced
2:00remaining
Identify 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"))
AReturns empty string
BTypeError
CIndexError
DReturns incorrect substring "BANC"
Attempts:
2 left
πŸ’‘ Hint
Check how the right pointer is used in slicing the string.
🧠 Conceptual
advanced
1:30remaining
Why Use a Sliding Window in Minimum Window Substring?
Why is the sliding window technique suitable for solving the Minimum Window Substring problem?
ABecause it sorts the string to find the substring faster
BBecause it allows checking all substrings by expanding and contracting the window efficiently
CBecause it uses recursion to explore all substring possibilities
DBecause it converts the string into a graph for traversal
Attempts:
2 left
πŸ’‘ Hint
Think about how to find the smallest substring containing all needed characters without checking every substring.
πŸš€ Application
expert
2:30remaining
Minimum Window Substring with Unicode Characters
Given the string s = "𝔸𝔹𝔸𝔻𝔸𝔹𝔠" and t = "𝔸𝔠", what is the minimum window substring containing all characters of t?
A"𝔸𝔹𝔸𝔻𝔸𝔹𝔠"
B"𝔠"
C"𝔹𝔠"
D"𝔸𝔹𝔠"
Attempts:
2 left
πŸ’‘ Hint
Unicode characters count as single characters; find the shortest substring containing both.