Javatpoint Logo
Javatpoint Logo

Minimum Window Substring in Java

Two strings S1 and S2 are given to us. Our task is to find substring str such that S2 is a subsequence of str. If there are multiple valid substrings, then the substrings of the minimum size should be taken into consideration. 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. A subsequence is a sequence that is derived from another sequence by removing zero or more elements without changing the order.

Example 1:

Input

S1 = "JavaTpoint"

S2 = "Tin"

Output: The valid substring is "Tpoin".

Explanation: The string "Tpoin" contains all of the letters of the string "Tin".

Example 2:

Input

S1 = "qwertyiop"

S2 = "tip"

Output: The valid subsequence is "tyiop".

Explanation: The string "Tpoin" contains all of the letters of the string "Tin".

Approach: Two Pointers

First of all, we will look for a window where the complete string S2 can be found actually. 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. Then, we can return that window.

The following are the steps involved in finding the minimum window subsequence:

Maintain the two pointers ptr1 and ptr2 and assign value 0 to them. Ptr1 is for the string S1, and ptr2 is for the string S2.

Whenever S1[ptr1] == S2[ptr2], then both the pointers should be moved simultaneously; otherwise, move only the ptr1 pointer ahead.

When the value of ptr2 becomes equal to the length of the string S2, then the string S2 is found in the string S1. Now, we have to reduce the window size. Before reducing the window size, it is essential to know why unwanted characters can come into the window.

For example, let us consider the following strings, where S1 = "ypcdepdde", and S2 = "pde".

Initially, ptr1 and ptr2 are positioned at the 0 indexes of their respective strings. At the 0th index, there is a mismatch (y != p), so we move the pointer ptr1 by one step. Now, we move the pointers ptr1, as well as ptr2, simultaneously by one step. Now, c is getting compared with d, which is a mismatch. So, ptr1 increases with step 1. Now we see a match, and both pointers move ahead simultaneously. At the 4th index of the string S1, we get the ptr2 as three, which is the size of the string S2. Thus, we get the window of size 5. 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. The index where the ptr2 becomes 0 is where the boundary should be put. Anything outside the boundary is unwanted character.

So, earlier we got the window as "ypcde", after shrinking it we get "pcde". Thus, the character 'y' is unwanted.

The same process can be repeated for the rest part of the string S1.

FileName: MinWindowSubsequence.java

Output:

For the strings "JavaTpoint" and "Tin"
The minimum window is : Tpoin

For the strings "ypcdepdde" and "pde"
The minimum window is : pcde

For the strings "qwertyiop" and "tip"
The minimum window is : tyiop

Complexity Analysis: Because of the nested loops (one for-loop, another while-loop), the time complexity of the program is O(n2), where n is the total number of characters present in the string. No extra space is used by the program making the space complexity as O(1).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA