


Let S = “abdd” and X = “bd”.
The windows in S which contain all the characters in X are: 'abdd', 'abd', 'bdd', 'bd'.
Out of these, the smallest substring in S which contains all the characters present in X is 'bd'.
All the other substring have a length larger than 'bd'.
The very first line of input contains an integer T denoting the number of test cases.
The first line of every test case contains the string S.
The second line of every test case contains the string X.
For each test case, print the smallest window in S which contains all the characters present in X, in a separate line.
There is always a valid window in S which contains all the characters of X.
You do not need to print anything, it has already been taken care of. Just implement the given function.
In case of multiple answers, print only the substring that occurs first.
For example: for the string S = 'cbbbc' and X = 'bc', there are 2 possible answers i.e. 'cb' and 'bc', your code should print 'cb'.
1 <= T <= 10
1 <= |S|, |X| <= 10^5
Here, |S| denotes the length of string S and |X| denotes the length of string X.
Time Limit: 1 sec
A simple and intuitive approach could be to generate all the possible substrings of string S and choose the smallest substring which contains all the characters present in X.
This can be done by the following approach:
In this approach, we use the concept of two pointers along with a hashtable. The two pointers - start and end, represent the current substring (window) which is in consideration. We move the end pointer to increase the size of the window and move the start pointer to decrease the size of the window. When a valid window is found, shrink the window if possible, then record its width. The smallest valid window gives us the answer to the problem.
The idea is to consider the windows of the string S and check for validity; the window(S) is valid if all the characters of X are present in S.
The basic variables used are:
Start, stores the starting index.
End stores the ending index.
Counter, storing the count of characters in X which are not yet found in the window(S).
The algorithm for the approach is as follows:
Let us understand this by an example:
Let's assume that string S = “aaabca” and string X = “ac”.
As a part of the summary, we considered substrings: