Challenge - 5 Problems
Minimum Window Substring 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 Rust function when called with s = "ADOBECODEBANC" and t = "ABC"?
DSA Rust
fn min_window(s: String, t: String) -> String {
use std::collections::HashMap;
let mut need = HashMap::new();
for c in t.chars() {
*need.entry(c).or_insert(0) += 1;
}
let (mut left, mut right) = (0, 0);
let (mut start, mut min_len) = (0, usize::MAX);
let mut window = HashMap::new();
let mut valid = 0;
let s_chars: Vec<char> = s.chars().collect();
while right < s_chars.len() {
let c = s_chars[right];
right += 1;
if need.contains_key(&c) {
*window.entry(c).or_insert(0) += 1;
if window[&c] == need[&c] {
valid += 1;
}
}
while valid == need.len() {
if right - left < min_len {
start = left;
min_len = right - left;
}
let d = s_chars[left];
left += 1;
if need.contains_key(&d) {
if window[&d] == need[&d] {
valid -= 1;
}
*window.entry(d).or_insert(0) -= 1;
}
}
}
if min_len == usize::MAX {
"".to_string()
} else {
s_chars[start..start + min_len].iter().collect()
}
}Attempts:
2 left
💡 Hint
Think about the smallest substring that contains all characters of t.
✗ Incorrect
The minimum window substring that contains all characters 'A', 'B', and 'C' in "ADOBECODEBANC" is "BANC".
🧠 Conceptual
intermediateUnderstanding the Sliding Window Technique
In the context of the Minimum Window Substring problem, what is the main purpose of the sliding window technique?
Attempts:
2 left
💡 Hint
Think about how the window changes size and position.
✗ Incorrect
The sliding window technique moves the window boundaries to include or exclude characters, aiming to find the smallest substring that contains all characters of t.
🔧 Debug
advancedIdentify the Error in Rust Code for Minimum Window Substring
What error will occur when running this Rust code snippet for minimum window substring?
let s = "a".to_string();
let t = "aa".to_string();
let result = min_window(s, t);
println!("{}", result);
DSA Rust
fn min_window(s: String, t: String) -> String {
use std::collections::HashMap;
let mut need = HashMap::new();
for c in t.chars() {
*need.entry(c).or_insert(0) += 1;
}
let (mut left, mut right) = (0, 0);
let (mut start, mut min_len) = (0, usize::MAX);
let mut window = HashMap::new();
let mut valid = 0;
let s_chars: Vec<char> = s.chars().collect();
while right < s_chars.len() {
let c = s_chars[right];
right += 1;
if need.contains_key(&c) {
*window.entry(c).or_insert(0) += 1;
if window[&c] == need[&c] {
valid += 1;
}
}
while valid == need.len() {
if right - left < min_len {
start = left;
min_len = right - left;
}
let d = s_chars[left];
left += 1;
if need.contains_key(&d) {
if window[&d] == need[&d] {
valid -= 1;
}
*window.entry(d).or_insert(0) -= 1;
}
}
}
if min_len == usize::MAX {
"".to_string()
} else {
s_chars[start..start + min_len].iter().collect()
}
}Attempts:
2 left
💡 Hint
Consider what happens when t has more occurrences of a character than s.
✗ Incorrect
Since s = "a" and t = "aa", s does not have enough 'a's to cover t, so the function returns an empty string.
🚀 Application
advancedMinimum Window Substring with Unicode Characters
Given the input strings s = "あいうえおかきくけこ" and t = "きえお", what is the minimum window substring returned by the function?
Attempts:
2 left
💡 Hint
Remember the function works with Unicode characters as well.
✗ Incorrect
The smallest substring containing 'き', 'え', and 'お' is "えおかき".
🧠 Conceptual
expertTime Complexity of Minimum Window Substring Algorithm
What is the time complexity of the sliding window algorithm used to solve the Minimum Window Substring problem?
Attempts:
2 left
💡 Hint
Consider how many times each pointer moves in the sliding window.
✗ Incorrect
Each character in s is visited at most twice by the left and right pointers, resulting in O(n) time complexity.
