
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
Editorial
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. |
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