You are given two strings ‘A’ and ‘B’ of length ‘N’ and ‘M’ respectively. You need to construct a new string ‘C’ by merging both strings in such a way that the relative order of characters in ‘A’ and ‘B’ is the same in string ‘C’.
For example, if the string ‘A’ is of length 3 then the first character of string ‘A’ should be placed before the second and third characters of string ‘A’ in string ‘C’. Similarly, the second character of string ‘A’ should be placed after the first character and before the third character of string ‘A’ in string ‘C’.
The cost of merging the strings is given by the number of consecutive characters in string ‘C’ that are not equal + 1, i.e. ‘C[ i ] != ‘C[ i + 1]’.
Find the minimum cost of merging both strings.
Example:Input: ‘N’ = 3, ‘M’ = 3, ‘A’ = aba, ‘B’ = aab
Output: 3
One of the valid strings with minimum cost after merging ‘A’ and ‘B’ is ‘aaabba’. The order of characters of string ‘A’ as well as ‘B’ is maintained in the string ‘aaabba’ and the cost of this string is (1 + 2) = 3.
The first line will contain the integer 'T', denoting the number of test cases.
The first line of each test case contains two integers ‘N’ and ‘M’ denoting the length of strings ‘A’ and ‘B’ respectively’.
The second line of each test case contains the string ‘A’.
The third line of each test case contains the string ‘B’.
Output format :
For each test case, you don’t need to print anything just return the minimum cost of merging strings ‘A’ and ‘B’.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 5000
1 <= M <= 5000
Sum of N Over all the Test cases <= 5000
Sum of M Over all the Test cases <= 5000
Time Limit: 1 sec
2
4 4
aaaa
bbbb
3 3
abc
def
2
6
For the first case:
One of the valid strings with minimum cost is “aaaabbbb”. The cost of this string is ( 1 + 1 ) = 2.
For the second case:
One of the valid strings with minimum cost is “abcdef”. The cost of this string is ( 1 + 5 ) = 6
2
3 4
ahu
huaa
4 3
abaa
aab
4
3
Can you think of a way to generate all possible strings recursively and then choose the one with minimum cost?
In this approach, We can recursively merge the strings. Since we need to merge the strings in such a way that their relative order is maintained, we can have two pointers ‘AIDX’ and ‘BIDX’ pointing to the first characters of strings ‘A’ and ‘B’ respectively. At any point, we have two options, either we merge the character at ‘A[ AIDX ]’ or we merge the character at ‘B[ BIDX ]’.
We can recursively try both ways one by one and check which one yields the minimum cost. To check the number of consecutive characters that are not equal we can make a variable that denotes the last character was taken from which string and then we can access the character and check if the current merging character is equal to the previous character or not.
The steps are as follows:-
// Recursive function to find minimum cost
function getMinCostUtil(string& a, string& b, int aidx, int bidx, int lastCh):
function getMinCost(string& a, string& b):
O( 2^( N + M ) ), Where ‘N’ and ‘M’ are the lengths of the strings ‘A’ and ‘B’ respectively.
For any ‘i’th character in ‘C’, we have two choices of either merging the character of ‘A’ or the character of ‘B’. We are exploring both the ways recursively, so the recursive tree grows as 1, 2, 4, … 2^( N + M) because the length of the string should be ( ‘N’ + ‘M’ ).
Hence the time complexity is O( 2^( N + M ) ).
O( N + M ), Where ‘N’ and ‘M’ are the lengths of the strings ‘A’ and ‘B’ respectively.
The height of the recursive tree can be at most ( ‘N’ + ‘M’ ).
Hence space complexity is O( N + M ).