You are given alphanumeric strings s and t. Find the minimum window (substring) in s which contains all the characters of t.

Input:

AYZABOBECODXBANC

ABC

Output: BANC

The minimum window is "BANC", which contains all letters - A B and C. We cannot find a window of smaller length than “BANC”.

Input:

BACRDESDFBAER

BAR

Output: BACR

Here, we can see that there are 2 smallest windows - “BACR” and “BAER”. However, the output is “BACR” because it is the leftmost one.

Input Parameters: There are 2 arguments in the input, a string named s and a string named t.

Output: Return a string result, which is the minimum window (substring) in string s that contains all characters of string t. If no such window exists, then return an “-1” string and if there are multiple minimum windows of the same length, then return the leftmost window.

Constraints:

● 1

● 1

**Solutions:**

We provided two solutions.

This is a brute force approach. We check whether each substring of string s is a valid window or not. If we find it to be a valid window, we update our result accordingly.

O(n^3) where n is the length of string s.

As we are checking for all substrings and as there are O(n^2) substrings and we take O(n) time to check whether the particular substring can be a valid window, so total complexity is O(n^3).

O(1).

We create frequency arrays of size O(128) to count the occurrence of each character present in strings t and s. So overall complexity is O(1).

O(n+m) where n is the length of string s and m is the length of string t.

For storing input it will take O(n+m), as we are storing two strings of length n and length m and the auxiliary space used is O(1) hence total complexity will be O(n+m).

**Note:** We could use an array of length 62 (with some mapping) instead of 128, but this is a general solution which works for the input string containing any ASCII characters.

In this approach, we create an array named frequency to keep a count of occurrences of each character in string t (O(length of t)). Now we start traversing the string S and keep a variable “cnt” which increases whenever we encounter a character present in string t. When the value of count reaches the length of t, this substring contains all the characters present in string t. We try removing extra characters as well as unwanted characters from the beginning of the obtained string. The resultant string is checked whether it can become the minimum window, and the answer is updated accordingly.

This algorithm uses the 2 pointer method, which is widely used in solving various problems. You can refer https://www.geeksforgeeks.org/two-pointers-technique/ article to get an idea about this method as well as see related problems where this method can be applied.

O(n) where n is the length of string s.

Since each character of string s is traversed at most 2 times, the time complexity of the algorithm is O(n) + O(m).

O(1).

We are creating 2 frequency arrays of size 128, which use extra space O(128) + O(128). Hence it is O(1).

O(n+m) where n is the length of string s and m is the length of string t.

For storing input it will take O(n+m), as we are storing two strings of length n and length m and the auxiliary space used is O(1) hence total complexity will be O(n+m).

**Note:** We could use an array of length 62 (with some mapping) instead of 128, but this is a general solution which works for the input string containing any ASCII characters.