Lexicographically largest possible by merging two strings by adding one character at a time | C ++ program for the approach ; Recursive bfunction for finding the lexicographically largest string ; If either of the string length is 0 , return the other string ; If s1 is lexicographically larger than s2 ; Take first character of s1 and call the function ; Take first character of s2 and recursively call function for remaining string ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string largestMerge ( string s1 , string s2 ) { if ( s1 . size ( ) == 0 || s2 . size ( ) == 0 ) return s1 + s2 ; if ( s1 > s2 ) { return s1 [ 0 ] + largestMerge ( s1 . substr ( 1 ) , s2 ) ; } return s2 [ 0 ] + largestMerge ( s1 , s2 . substr ( 1 ) ) ; } int main ( ) { string s1 = " geeks " ; string s2 = " forgeeks " ; cout << largestMerge ( s1 , s2 ) << endl ; return 0 ; }