
This step is important as we will use it later to check if we have found the minimum substring.

Step 2: Determine the length of the target string. The other hash table will store the frequency of each character in the sliding window.

#MINIMUM WINDOW SUBSTRING JAVA HOW TO#
Here is a detailed solution on how to solve the Minimum Window Substring problem on LeetCode: This problem can be solved using a sliding window technique and hashing. Minimum Window Substring problem is a famous problem on LeetCode that requires you to find the smallest window in a string that contains all the characters of another string. MinWindowSubsequence obj = new MinWindowSubsequence() creating an object of the class DiffSubseqGCD returning our final answer, which is stored in the window we have found the window of lesser length Public static String findMinWindow(String S1, String S2) The same process can be repeated for the rest part of the string S1. So, earlier we got the window as "ypcde", after shrinking it we get "pcde". Anything outside the boundary is unwanted character. The index where the ptr2 becomes 0 is where the boundary should be put. Now, when we start traversing the elements in the window from right to left and when there is a match, we reduce the pt2 by 1. At the 4th index of the string S1, we get the ptr2 as three, which is the size of the string S2. Now we see a match, and both pointers move ahead simultaneously. Now, c is getting compared with d, which is a mismatch. Now, we move the pointers ptr1, as well as ptr2, simultaneously by one step. At the 0th index, there is a mismatch (y != p), so we move the pointer ptr1 by one step. Initially, ptr1 and ptr2 are positioned at the 0 indexes of their respective strings. Before reducing the window size, it is essential to know why unwanted characters can come into the window.įor example, let us consider the following strings, where S1 = "ypcdepdde", and S2 = "pde". When the value of ptr2 becomes equal to the length of the string S2, then the string S2 is found in the string S1. Whenever S1 = S2, then both the pointers should be moved simultaneously otherwise, move only the ptr1 pointer ahead. Ptr1 is for the string S1, and ptr2 is for the string S2. Maintain the two pointers ptr1 and ptr2 and assign value 0 to them. The following are the steps involved in finding the minimum window subsequence: After that, we will try to shrink the window as much as we can while keeping in mind that the window must contain all the characters of the string S2. Approach: Two Pointersįirst of all, we will look for a window where the complete string S2 can be found actually. Output: The valid subsequence is "tyiop".Įxplanation: The string "Tpoin" contains all of the letters of the string "Tin". A subsequence is a sequence that is derived from another sequence by removing zero or more elements without changing the order.Įxplanation: The string "Tpoin" contains all of the letters of the string "Tin".

If multiples valid substrings of the same size are present, then any one of the valid substrings of the string S1 can be taken into consideration. If there are multiple valid substrings, then the substrings of the minimum size should be taken into consideration. Our task is to find substring str such that S2 is a subsequence of str. Next → ← prev Minimum Window Substring in Java
