×

Minimum Window Subsequence in Java

In this article, you will be very well acknowledged about the minimum window subsequence, what is the approach and how it is implemented. The example program is also executed and its output is also displayed.

Problem Statement

Find the smallest (contiguous) substring W in string S such that T is a subset of W given strings S and T.

Return the null string "if there isn't a window in S that includes all the characters in T." Return the minimum-length window with the leftmost starting index if there are numerous ones.

Example 1:

Input:

S = asdfghjkl   T = fhl

Output:

The valid output string is W = fghjkl

The string fghjkl includes every letter in the stringfhl.

Example 2 :

Input:

S = zxcvbnm  T = vbm

Output:

The valid output string is W = vbnm

The string vbnm includes every letter in the string vbm

Methodology

First, we'll search for a window that has the entire string T in its actual form. The window will then be reduced as much as it can be while still retaining the requirement that it contain every character in the string T. After that, we can close the window.

The steps for determining the minimal window subsequence are as follows:

Keep the two references ptr1 as well as ptr2 and give them the value 0. Ptr1 and Ptr2 are for the strings S and T, respectively.

When S[ptr1] == T[ptr2], both points should be advanced at once; otherwise, just the ptr1 pointer should be advanced.

The string T is located within string S because when value of ptr2 reaches the size of the string T. We must now make the window smaller. It is crucial to understand why unnecessary characters may enter the window before adjusting the window size.

Let's use the following strings as an example, with S = "ypcdepdde" and T = "pde".

Ptr1 and Ptr2 be placed at the 0 indices of their corresponding strings at the beginning. Since there is an error (y!= p) at index 0, we advance the pointer ptr1 through one position. We now simultaneously move pointers ptr1 and ptr2 by one step. Now, c and d are being compared, which is an incorrect match. Thus, step 1 causes ptr1 to grow. Now when there is a match, both pointers advance at once. The size of a string T is revealed by the ptr2 at the fourth index of a string S, which is three. Thus, the window with size 5 is obtained. Now, when we move going right to left through the window's elements, if we find a match, we cut pt2 by 1. The border should be placed at the point in which the ptr2 becomes 0. Anything beyond the line is undesirable character.

As a result, after shrinking the window, we obtain "pcde" instead of "ypcde" before. Therefore, the character "y" is undesirable.

For the remaining portion of the string S, the same procedure can be repeated.

File name: WindowSeq.java

public class WindowSeq   
{  
  
public static String findMinWindow(String S1, String S2)   
{  
  
// The window is initially bare in the beginning.
String win = "";  
int ptr2 = 0;  
int minimum = S1.length() + 1;  
  
for (int ptr1 = 0; ptr1 < S1.length(); ptr1++)   
{  
  
// Since ptr1 is just the loop variable, it will automatically increase the ptr2 pointer if the //characters in both strings match.
if (S1.charAt(ptr1) == S2.charAt(ptr2))   
{  
ptr2 = ptr2 + 1;  
  
// The entire string S2 has already been examined. As a result, it is now appropriate to //close the window.
if (ptr2 == S2.length())   
{  
int e = ptr1 + 1;  
ptr2 = ptr2 - 1;  
  
// lowering the size of the window 
while (ptr2 >= 0)   
{  
if (S1.charAt(ptr1) == S2.charAt(ptr2))   
{  
ptr2 = ptr2 - 1;  
}  
ptr1 = ptr1 - 1;  
}  
  
ptr2 = ptr2 + 1;  
ptr1 = ptr1 + 1;  
  
// We must alter the window because we discovered one that is shorter.
if (e - ptr1 < minimum)   
{  
  
// the var minimum being updated
minimum = e - ptr1;  
  
// upgrading the window  
win = S1.substring(ptr1, e);  
}  
}  
}  
}  
      
// returning our final response and saving it in the window
return win;  
}  
  public static void main(String argvs[])  
{  
WindowSeq obj = new WindowSeq();  
String s1 = "zxcvbnm";  
String s2 = "vnm";  
System.out.println("For the given strings \"" + s1 + "\" and \"" + s2 + "\"");  
  
String str = obj.findMinWindow(s1, s2);  
System.out.println("The minimum window is : " + str);  
System.out.println();  
    
s1 = "ypcdepdde";  
s2 = "pde";  
System.out.println("For the given strings \"" + s1 + "\" and \"" + s2 + "\"");  
str = obj.findMinWindow(s1, s2);  
System.out.println("The minimum window is : " + str);  
System.out.println();  
    
s1 = "qwertyiop";  
s2 = "tip";  
System.out.println("For the given strings \"" + s1 + "\" and \"" + s2 + "\"");  
str = obj.findMinWindow(s1, s2);  
System.out.println("The minimum window is : " + str);  
  
}  
  
} 

Output

For the given strings “zxcvbnm” and “vnm”
The minimum window is: vbnm
For the given strings “asdfghjkl” and “fhl”
The minimum window is: fghjkl
For the given strings “qwertyuiop” and “tip”
The minimum window is: tyiop

The program's time complexity is O(n2), where n represents the total amount of characters in the string, due to the loops (one for-loop and another while-loop). The program does not consume any additional space, increasing the space's complexity to O (1).


Related Topics

Java Map Generic

Java arrays maintain an ordered collection of things, and the index can be used to access the data (an integer). Unlike HashMap, which stores data as a Key/Value pair. We...

3 minutes read.

Java File Extension

A computer file's suffix is called its file extension. It is easily recognized since it appears immediately after a period (.) in the file name. Consider the file Demo.java as an...

3 minutes read.

Java Integer signum() method

The signum() method of Java Integer class returns the signum function of the specified int value. Syntax public static int signum (int i)  Parameters The parameter ‘i’ represents the value whose signum is to...

1 minute read.

ArrayList VS Linked List

ArrayList VS Linked List Both the ArrayList and LinkedList implements the List interface, and they have some differences as well as some similarities between them. The internal working and performance of both vary significantly. Let’s...

7 minutes read.

Ad Hoc Problem on Arrays in Java

Ad hoc problems are issues that arise unexpectedly and require immediate attention. These problems can range from small and straightforward issues to more complex and time-consuming problems. Examples of ad...

3 minutes read.

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

How to reverse a linked list in java

The process of reversing a linked list in Java will be covered in this section. One of the most common questions in interviews is about reversing a linked list. If...

11 minutes read.

Types of Bitwise Operators in Java

In this tutorial, we will learn about the various types or kinds of bitwise operators in java. Before proceeding to bitwise operators, let us know what is meant by the...

6 minutes read.

Java Editors

A straightforward text editor may be used to create Java applications. However, a Java integrated programming environment (IDE) enables the software developer to create programs more quickly. An IDE offers...

4 minutes read.

Java Stringjoiner Class

StringJoiner is a class which is used to construct a sequence of characters which are separated by a delimiter. Optionally, it starts with a provided prefix and ended with the...

5 minutes read.

How to Develop Programming Logic in Java?

Introduction In the world of software development, Java programming language is one of the most powerful programming languages that is used to create a wide range of applications. It includes desktop,...

18 minutes read.

Java Plot

Java Plot is a phrase in Java that is mostly used for plotting coordinates on a cartesian plane. Plotting graphs in Java is accomplished through the use of various core...

3 minutes read.

Java array list remove time complexity

Java: We know that java is one of the programming languages. The main feature of java which is not in C or object oriented programming language is platform independence. Not only the...

7 minutes read.

Difference between throw and throws in java

This article shows you the core difference between “throw” and “throws”keywords in Java programming language.The throw keyword tells Java you want another part of the code to deal withthe exception,...

2 minutes read.

Java Integer equals() method

The equals() method of Integer class compares the given object to the specified object. Syntax public boolean equals(Object obj) Parameters The parameter ‘obj’ represents the object to be compared with. Overrides The equals() method overrides equals...

1 minute read.

Java Thread Creation

Java provides the two ways to create a Thread: Implementing the Runnable interface.Extending the Thread class. Implementing Runnable interface The easiest way of creating a thread is to make a class that implements...

5 minutes read.

Prepared statement in Java

Prepared statement: A prepared statement is a statement that is pre-compiled SQL statement, and it is a sub-interface of a statement. Compared to other statement objects in java, Prepared Statement objects have...

3 minutes read.

Java Networking

Networking Networking is a way of communicating the devices. It is made up of various technologies like computers, switches, routers that are interconnected and share data among themselves. The two common network protocols: 1. TCP/IP TCP stands for...

10 minutes read.

Equidigital in Java

In this section, we will understand what is an equidigital number and how to write Java programs to locate them. It is commonly asked in academic settings and Java coding...

4 minutes read.

Java Delete File

There are two techniques to erase a record in Java: Utilizing File.delete() technique.Utilizing File.deleteOnExit() technique. Using File.delete() technique: In Java, we can erase a document by utilizing the File.delete() technique for File class....

2 minutes read.