

You have several balls on the table in the form of a string named 'BOARD'. The colors of the balls can be red(R), blue(B), green(G), white(W), and yellow(Y). You also have several balls in your hand in the form of a string named 'hand'. Each time, you can do the following operations:
1. Choose a ball from the string 'HAND', and insert it anywhere on the string 'BOARD'.
2. If there is a group of strictly more than 2 balls of the same color touching each other, remove them from the string 'BOARD'. Keep doing this until the string 'board' becomes empty or no more balls can satisfy this condition.
Your task is to find the minimum number of insertions required to make the 'BOARD' empty. If it is not possible to make the string 'BOARD' empty, then print -1.
Note:
1) Both strings will be non-empty and will only contain the characters ‘R’, ‘B’, ‘G’, ‘W’, and ‘Y’.
2) Initially, the string 'BOARD' won’t have more than 2 balls of the same colors touching each other.
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the T test cases follow.
The first line of each test case contains a string 'BOARD', as described in the problem statement.
The second line of each test case contains a string 'HAND', as described in the problem statement.
Output Format:
For each test case, print a single line containing a single integer denoting the minimum number of insertions required to make the string 'BOARD' empty or -1 if it is impossible to make the 'board' empty.
The output for each test case will be printed in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= |BOARD| <= 10
1 <= |HAND| <= 4
Where ‘T’ denotes the number of test cases.
Time Limit: 1 sec.
1
YYRR
YR
2
One of the possible order of insertions: YYRR -> YY[Y]RR -> RR -> RR[R] -> empty. You required 2 insertions. There is no possible solution with only one insertion.
1
RR
YG
-1
It is not possible to make the string board empty.
Think of a recursive solution.
The idea here is to use recursion. At each step, we plan to insert the remaining balls in the string hand to the string board. Now the question arises, in which position do we have to insert the ‘i’th character of the string hand? The answer is simple; we have to insert ‘i’th character to all the positions on board to get all possible answers. That’s why we are using the recursive approach. After inserting ‘i’th character to a particular position, we have to always remove all consecutive balls of the same colors if their count is more than 2. After removing, we have to call the function again with the new board and another string hand character. This process continues until the length of the string board will become zero. So, if the board becomes empty, then we return the count of the insertions. Or, if all the balls in hand are inserted, and we haven’t found a solution, then also return. But in this case, we have to return a big integer as we are finding the minimum of all the total insertions, and this case doesn’t end up with a possible solution. After returning, we have to insert ‘i’th character to the next position and again call the same function and follow the exact process.
Again a question arises that what will be the order we have to follow in the string hand? Is any random order leads us to a possible solution? Or is there any particular order available? Well, a one-word answer is to think greedily! First, find the count of each color ball on the string board. Now, sort the string hand according to these counts. The minimum count colors ball will come first, and the maximum count colors ball will come last. This is one of the orders which we have to follow. Another order is to reverse the string hand and call the recursive function. The output must be the minimum of the recursive function’s return value with the above two orders.
O(N ^ M), where ‘N’ is the size of the string board, and ‘M’ is the size of the string hand.
As we are using recursion and in each function call, we are inserting a string hand character in each position of the string board. Hence the overall time complexity will be O(N ^ M).
O(N), where ‘N’ is the size of the string board.
As we are creating a new string of size N after every insertion in the recursive function. Hence, the space complexity will be O(N).