Lexicographically largest possible by merging two strings by adding one character at a time | Recursive function 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
def largestMerge ( s1 , s2 ) : NEW_LINE INDENT if len ( s1 ) == 0 or len ( s2 ) == 0 : NEW_LINE INDENT return s1 + s2 NEW_LINE DEDENT if ( s1 > s2 ) : NEW_LINE INDENT return s1 [ 0 ] + largestMerge ( s1 [ 1 : ] , s2 ) NEW_LINE DEDENT return s2 [ 0 ] + largestMerge ( s1 , s2 [ 1 : ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s1 = " geeks " NEW_LINE s2 = " forgeeks " NEW_LINE print ( largestMerge ( s1 , s2 ) ) NEW_LINE DEDENT