Rainbow Window: Minimum Covering Substring
๐ฆ Rainbow Window: Minimum Covering Substring
Difficulty: Advanced Tags: sliding-window, hashmap, two-pointer, strings, optimization Series: CS 102: Problem Solving & Logic
Problem
In the mystical realm, rainbow windows reveal the shortest magical span containing all required runes. Find the minimum window in string s that contains all characters from string t (including their required frequencies).
Given two strings s and t, return the minimum substring of s that contains all characters from t (including duplicates). If no such substring exists, return an empty string ''.
Real-World Application
Minimum window substring appears in: - Text editors - find & highlight feature, search with wildcards - Genomics - finding DNA subsequences containing specific patterns - Data compression - identifying minimal encoding windows - Log analysis - finding log entries containing all required keywords - Search engines - ranking documents by keyword proximity - Network packet analysis - identifying frames with specific byte patterns - Cybersecurity - pattern matching in malware detection
Input
data = {
's': str, # Source string to search within
't': str # Target string with required characters
}
Output
str # Minimum window substring, or '' if none exists
Constraints
- 1 โค len(t) โค len(s) โค 100,000
- ASCII letters only (uppercase and lowercase)
- Must match character frequencies (multiplicities)
- Aim for O(n) solution with sliding window
Examples
Example 1: Basic Minimum Window
Input:
{'s': 'ADOBECODEBANC', 't': 'ABC'}
Output: 'BANC'
Explanation:
- Windows containing ABC:
- 'ADOBEC' (length 6) - contains A, B, C โ
- 'ODEBANC' (length 7) - contains A, B, C โ
- 'BANC' (length 4) - contains A, B, C โ shortest!
- Return 'BANC'
Example 2: Impossible (Not Enough Characters)
Input:
{'s': 'a', 't': 'aa'}
Output: ''
Explanation:
- s has only one 'a', but t requires two 'a's
- Impossible to satisfy requirement
- Return empty string
Example 3: Repeated Characters
Input:
{'s': 'aaabdabcefaecbef', 't': 'abc'}
Output: 'abc'
Explanation:
- Need one each of 'a', 'b', 'c'
- 'abc' at position 6-8 is the shortest window
- Other valid windows are longer
Example 4: Entire String
Input:
{'s': 'abc', 't': 'abc'}
Output: 'abc'
Explanation: - The entire string is the minimum (and only) valid window
Example 5: Multiple Occurrences
Input:
{'s': 'ABAACBAB', 't': 'AAB'}
Output: 'BAAC' or 'AACB' or 'ACBA'
Explanation: - Need 2 A's and 1 B - Multiple windows of length 4 exist - Any one is valid (problem asks for any minimum window)
Example 6: At the End
Input:
{'s': 'abcdefgABC', 't': 'ABC'}
Output: 'ABC'
Explanation: - Minimum window is at the very end - Length 3
What You'll Learn
- Sliding window technique with dynamic expansion/contraction
- Using hash maps to track character frequencies
- Two-pointer approach for optimization
- How to maintain a valid window invariant
- Handling character multiplicities (duplicates)
Why This Matters
This is a top-tier coding interview question (LeetCode Hard) that tests: - Advanced string manipulation - Hash map proficiency - Sliding window pattern mastery - Optimization skills (O(n) solution) - Edge case handling
Companies that ask this: Google, Facebook, Amazon, Microsoft, Uber
Starter Code
def challenge_function(data):
"""
Find minimum window in s that contains all characters from t.
Args:
data: dict with keys:
- 's' (str): source string to search within
- 't' (str): target string with required characters
Returns:
str: minimum window substring, or '' if none exists
"""
# Your implementation here
pass