


You are given two strings named str1 and str2 of length N and M respectively. Find whether string str2 contains one of the permutations of string str1 as its substring. In other words, whether there exists any permutation of string str1 that is a substring of str2.
For Example :
Input: str1 = “ab” , str2 = “aoba”
Output: True
Explanation : Permutations of first-string str1 i.e. “ab” are [ “ab”, “ba” ].
The substrings of str2 i.e. “aoba” are [ “a”, “o”, “b”, “a”, “ao”, “ob”, “ba”, “aob”, “oba”, “aoba” ].
The string “ba” is present in the list of substrings of string str2.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘T’ lines represent the ‘T’ test cases.
The first and only line of each test case contains two single space-separated strings str1 and str2.
Output format :
For each test case, print either “True” or “False”.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^4
1 <= M <= 10^4
Strings ‘str1’ and ‘str2’ consists only of lower case letters.
Time limit: 1 second
2
ab ebbao
b acd
True
False
In the First Test Case :
Permutations of first-string str1 i.e. “ab” are [ “ab”, “ba” ].
The substrings of str2 i.e. “ebbao” are [ “e”, “b”, “b”, “a”, “o”, “eb”, “bb”, “ba”, “ao”, “ebb”, “bba”, “bao”, “ebba”, “bbao”, “ebbao” ].
The string “ba” is present in the list of substrings of string str2.
In the Second Test Case :
Permutations of first-string str1 i.e. “b” are [ “b” ].
The substrings of str2 i.e. “acd” are [ “a”, “c”, “d”, “ac”, “cd”, “acd” ].
The string “b” is not present in the list of substrings of string str2.
2
abc bdbkcera
abc cb
False
False
In the Second Test Case :
Permutations of first-string str1 i.e. “abc” are [ “abc”, “bac”, “acb”, “bca”, “cab”, cba” ].
The substrings of str2 i.e. “cb” are [ “c”, “b”, “cb” ].
The string of any of the permutations of “abc” is not present in the list of substrings of string str2.
Brute-Force Method
O(N!), where N is the size of the small string.
In the worst case, we will be generating all the permutations of str1 i.e N! and checking for each permutation whether it exists in str2 or not.
O(N^2), where N is the size of the small string.
As the depth of the recursion tree is N. Every node of the recursion tree contains a string of maximum length N.