


If ‘S’= “great” and ‘W’= “tagre”, ‘S’ can be rearranged to form ‘W’.
‘S’ = “gre” + “at” (We choose to swap ‘X’ & ‘Y’)
‘S’ = “at” + “gre”
Now letting “gre” as it is and applying operation on “at”.
‘S’ = “a” + “t” + “gre”
Swapping “a” and “t”
‘S’ = “t” + “a” + “gre”
Therefore ‘S’ can be rearranged into ‘W’.
Both strings are of the same length and operations can only be applied to string ‘S’.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the length of the string ‘S’ and ‘W'.
The next line of the test case contains two space-separated strings ‘S’ and ‘W’.
For each test case print ‘True’ if ‘S’ can be rearranged to form ‘W’ else print ‘False’.
You do not need to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 50
1 <= N <= 20
Time Limit: 1sec
We will check through all the possible combinations in which ‘S’ can be rearranged:
We are working with breaking the string into two substrings and again joining them in some order. We can use memoization as we are checking the same pair of substrings multiple times. Thus this problem has optimal substructure and overlapping subproblems.
Therefore we can solve the problem by following dynamic programming algorithm: