Let us suppose we have two strings 'a' and 'b' and a string 'merge'. The task is to fill the string 'merge' with the characters from 'a' and 'b' in such a way that,
a = “bacaa”b = “abcaa”
Since the given string 'a' is lexicographically greater than the string 'b', we will extract the first character from string 'a', i.e., “b” and then from string 'b'. After extracting, the string will be “babcacaaaa”.
Approach to solve this Problem
The recursive approach to solve this problem is that we will extract each character of string 'a' and string 'b' and will check if the characters of string 'a' are lexicographically greater than the other string and finally concatenate to string 'merge'.
We will find the substring of each character after a number of positions and concatenate into 'merge' if it is lexicographically greater than the other string.
def concatenateLargest(a, b): ans = "" while a and b: if a > b: ans = a a = a[1:] else: ans = b b = b[1:] ans = a ans = b return ans a = "bacaa" b = "abcaa" print(concatenateLargest(a, b))
Running the above code will generate the output as,
The two strings “bacaa” and “abcaa” will become “bacabcaaaa” after merging according to the given problem.