Tutorialspoint
Problem
Solution
Submissions

Minimum Window Substring

Certification: Advanced Level Accuracy: 0% Submissions: 0 Points: 15

Write a C++ program to solve the Minimum Window Substring problem. Given two strings `s` and `t`, return the minimum window substring of `s` that contains all characters of `t` (including duplicates) in any order. If there is no such substring, return the empty string `""`.

Example 1
  • Input: s = "ADOBECODEBANC", t = "ABC"
  • Output: "BANC"
  • Explanation:
    • Step 1: Create a frequency map of characters in string t.
    • Step 2: Use a sliding window approach on string s to find windows containing all characters from t.
    • Step 3: Keep track of the minimum valid window found.
    • Step 4: The minimum window substring "BANC" contains all characters from "ABC".
Example 2
  • Input: s = "a", t = "a"
  • Output: "a"
  • Explanation:
    • Step 1: Create a frequency map of characters in string t.
    • Step 2: Use a sliding window approach on string s to find windows containing all characters from t.
    • Step 3: The entire string "a" already contains all characters from t.
    • Step 4: Return "a" as the minimum window substring.
Constraints
  • 1 ≤ s.length, t.length ≤ 10^5
  • s and t consist of uppercase and lowercase English letters
  • The answer is guaranteed to be unique
  • Time Complexity: O(n + m) where n is the length of s and m is the length of t
  • Space Complexity: O(k) where k is the size of the character set
StringsAirbnbTutorix
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use the sliding window technique with two pointers
  • Maintain a frequency map of characters in t
  • Expand the right pointer until all required characters are found
  • Contract the left pointer to minimize the window size
  • Keep track of the minimum valid window found so far

Steps to solve by this approach:

 Step 1: Create a frequency map for characters in string t.
 Step 2: Initialize two pointers (left and right) defining the sliding window.
 Step 3: Move the right pointer to expand the window until it contains all characters of t.
 Step 4: Once all required characters are found, move the left pointer to contract the window.
 Step 5: Update the minimum window length whenever a valid window is found.
 Step 6: When the left pointer moves, update character counts and required status.
 Step 7: Return the substring corresponding to the minimum window found.

Submitted Code :