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: