Tutorialspoint
Problem
Solution
Submissions

Minimum Window Substring

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

Write a C program to find the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such window in s that covers all characters in t, return the empty string "". If there are multiple valid windows, return the one with the smallest length.

Example 1
  • Input: s = "ADOBECODEBANC", t = "ABC"
  • Output: "BANC"
  • Explanation:
    • Step 1: The minimum window substring "BANC" includes one 'A', one 'B', and one 'C' from string t.
    • Step 2: This is the shortest substring that contains all characters of t.
    • Step 3: Therefore, the answer is "BANC".
Example 2
  • Input: s = "a", t = "aa"
  • Output: ""
  • Explanation:
    • Step 1: String t requires two 'a' characters.
    • Step 2: String s only contains one 'a' character.
    • Step 3: Therefore, no valid window exists, return empty string.
Constraints
  • 1 ≤ s.length, t.length ≤ 10^5
  • s and t consist of uppercase and lowercase English letters
  • Time Complexity: O(|s| + |t|)
  • Space Complexity: O(|s| + |t|)
AlgorithmsHCL TechnologiesApple
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 sliding window technique with two pointers (left and right).
  • Maintain frequency count of characters in t using a hash map.
  • Expand window by moving right pointer until all characters of t are covered.
  • Contract window by moving left pointer while maintaining validity.
  • Keep track of minimum window length and starting position.
  • Use a counter to track how many unique characters are fully matched.

Steps to solve by this approach:

 Step 1: Create frequency map for all characters in string t and count unique characters needed.
 Step 2: Use two pointers (left and right) to create a sliding window over string s.
 Step 3: Expand window by moving right pointer and update character frequencies.
 Step 4: When window contains all required characters, try to contract from left.
 Step 5: While contracting, keep track of the minimum valid window found.
 Step 6: Remove character from left side and update frequencies and validity counter.
 Step 7: Continue until right pointer reaches end, then return the minimum window found.

Submitted Code :