Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find minimum string size that contains given substring in Python
Given two strings s and t, we need to find the length of the minimum substring in s that contains all characters of t. If no such substring exists, we return -1. This is known as the Minimum Window Substring problem.
For example, if s = "thegrumpywizardmakes" and t = "wake", the shortest substring containing all characters of "wake" is "wizardmake" with length 10.
Algorithm
We use the sliding window technique with two pointers ?
Create a frequency counter for characters in string t
Use two pointers (start and end) to maintain a sliding window
Expand the window by moving end pointer until all characters are found
Contract the window by moving start pointer while maintaining all characters
Track the minimum window size during contraction
Implementation
class Solution:
def solve(self, s, t):
# Count frequency of characters in t
counter = {}
for char in t:
counter[char] = counter.get(char, 0) + 1
start = 0
min_length = float("inf")
required_chars = len(counter)
for end in range(len(s)):
current = s[end]
# If current character is in t, decrease its count
if current in counter:
counter[current] -= 1
if counter[current] == 0:
required_chars -= 1
# Try to shrink window from left
while required_chars == 0:
prev_char = s[start]
# Update minimum length
min_length = min(min_length, end - start + 1)
if prev_char in counter:
counter[prev_char] += 1
if counter[prev_char] > 0:
required_chars += 1
start += 1
return min_length if min_length != float("inf") else -1
# Test the solution
solution = Solution()
s = "thegrumpywizardmakes"
t = "wake"
result = solution.solve(s, t)
print(f"Minimum substring length: {result}")
Minimum substring length: 10
How It Works
Let's trace through the example step by step ?
s = "thegrumpywizardmakes"
t = "wake"
# Character frequencies in t: {'w': 1, 'a': 1, 'k': 1, 'e': 1}
# We need to find all 4 distinct characters
# When we reach "wizardmake" (indices 9-18):
# - Contains: w=1, i=1, z=1, a=2, r=1, d=1, m=1, k=1, e=1
# - Has all characters from "wake": w=1, a=1, k=1, e=1
# - Length = 10
substring = "thegrumpywizardmakes"[9:19]
print(f"Found substring: '{substring}'")
print(f"Length: {len(substring)}")
# Verify it contains all characters from t
for char in t:
if char in substring:
print(f"'{char}' found in substring")
Found substring: 'wizardmake' Length: 10 'w' found in substring 'a' found in substring 'k' found in substring 'e' found in substring
Time and Space Complexity
| Aspect | Complexity | Explanation |
|---|---|---|
| Time | O(|s| + |t|) | Each character visited at most twice |
| Space | O(|t|) | Counter dictionary stores t's characters |
Conclusion
The sliding window technique efficiently solves the minimum window substring problem in linear time. We expand the window to include all required characters, then contract it to find the minimum length while maintaining coverage.
