Given two strings ‘initial’ and ‘final’ , where ‘initial’ is the initial string and ‘final’ is the final string. Each state will contain ‘a’,’b’ and only one empty slot represented by ‘_’. Your task is to transform the initial string into the final string with the minimum number of operation.
Allowed operations are:
1. You can swap empty character with any adjacent character. (For example, ‘aba_ab’ can be converted into ‘ab_aab’ or ‘abaa_b’).
2. You can swap empty character with next to the adjacent character only if the adjacent character is different from next to the adjacent character. (For example, ‘aba_ab’ can be converted into ‘a_abab’ or ‘ababa_’, but ‘ab_aab’ cannot be converted to ‘abaa_b’ because ‘a’ cannot jump over ‘a’).
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘2*T’ lines represent the ‘T’ test cases.
The first line of each test case consists of a string ‘initial’ denoting the initial string.
The second line of each test case consists of a string ‘final’ denoting the final string.
Output Format :
For each test case, print a single integer denoting the minimum number of operations to convert the initial string to the final string.
Note :
1. Do not print anything, it has already been taken care of. Just implement the given function.
2. The string characters consist of ‘a’ ‘b’ and one ‘_’.
3. Length of the initial string is equal to the length of the final string.
1 <= T <= 25
1<= length(initial), length(final) <= 10
Time limit: 1 sec
2
a_b
ab_
aba_a
_baaa
1
2
Test Case 1:
The initial string is: a_b
The final string is ab_
We can clearly see that using the first operation i.e swapping the second and third character we can get the final string ab_.
Therefore we return 1.
Test Case 2:
The initial string is: aba_a
The final string is: _baaa
We need 2 operations to change the initial string to final string i.e :
(Consider 0 based indexing)
1.Swap index 2 and index 3 (Allowed since they are adjacent)
After the operation, the string is: ab_aa
2. Swap index 0 and index 2 ( Allowed since ‘_’ is at index 2 and the elements at index 1 and 0 are different.
After the operation, the string is: _baaa
Which is the final string.
Hence the minimum of 2 operations are needed.
2
ab_bba
bbbaa_
aa_ab
b_aaa
6
6
Use a Breadth-first search to find the optimal answer.
For example, let us take the initial string ‘a_ba’ and final string ‘baa_’ a small part of the graph will look like this:
The lines in red signify the shortest path. We can clearly see that we need 3 operations to convert the initial string to the final string.
Keeping all this in mind we can design the following approach:
O(4^N), Where ‘N’ denotes the number of elements present in the string.
In the worst case, we can have 4 unique possible strings after every operation, therefore the number of nodes in the graph will be 4^N. Since BFS takes linear time, the time complexity will be 4^N.
O(N*4^N). Where ‘N’ denotes the number of elements present in the string.
Since we maintain a map of strings and in the worst case we can have 4^N unique strings, therefore, the space complexity will be N*4^N.