Tutorialspoint
Problem
Solution
Submissions

Minimum Window Substring

Certification: Advanced Level Accuracy: 100% Submissions: 1 Points: 15

Write a Java program to find the minimum window substring of string S which contains all the characters of string T. If there is no such substring, return an empty string.

A window substring is a contiguous sequence of characters within the given string. The minimum window substring is the shortest such substring that contains all characters from T, including duplicates.

Example 1
  • Input: S = "ADOBECODEBANC", T = "ABC"
  • Output: "BANC"
  • Explanation: Create frequency maps for the characters in T. Start with a window covering the whole string "ADOBECODEBANC". Shrink from the left as much as possible while ensuring all characters from T are still included. The window "ADOBEC" contains all characters from T. Continue moving the window and find "ODEBANC" which also contains all characters from T. Finally, find "BANC" which is the minimum window containing all characters from T. Therefore, "BANC" is the answer.
Example 2
  • Input: S = "a", T = "aa"
  • Output: ""
  • Explanation: S contains only one 'a' but T needs two 'a's. Since S doesn't have enough 'a's to satisfy T's requirements, there is no valid window. Return an empty string "".
Constraints
  • 1 <= S.length, T.length <= 10^5
  • S and T consist of uppercase and lowercase English letters
  • Time Complexity: O(n) where n is the length of string S
  • Space Complexity: O(k) where k is the size of the character set
StringsWalmartApple
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 (left and right)
  • Maintain a frequency map of characters needed from T
  • Move the right pointer to include characters and expand the window
  • When all required characters are found, move the left pointer to minimize the window
  • Keep track of the smallest valid window found so far
  • Use a counter to track how many unique characters from T have been satisfied

Steps to solve by this approach:

 Step 1: Create a frequency map (targetMap) to keep track of the characters we need from string T.

 Step 2: Initialize two pointers, left and right, both starting at the beginning of string S.
 Step 3: Move the right pointer to expand the window and include characters until we have all required characters from T.
 Step 4: Once we have all required characters, move the left pointer to shrink the window as much as possible while still maintaining all required characters.
 Step 5: Keep track of the minimum valid window found during this process.
 Step 6: Continue this sliding window approach until the right pointer reaches the end of string S.
 Step 7: Return the minimum window found, or an empty string if no valid window exists.

Submitted Code :