Lexicographically largest possible by merging two strings by adding one character at a time | 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 ; Given Input ; Function Call
const largestMerge = ( s1 , s2 ) => { if ( s1 . length == 0 s2 . length == 0 ) return s1 + s2 ; if ( s1 > s2 ) { return s1 [ 0 ] + largestMerge ( s1 . substr ( 1 ) , s2 ) ; } return s2 [ 0 ] + largestMerge ( s1 , s2 . substr ( 1 ) ) ; } s1 = " " ; s2 = " " ; document . write ( largestMerge ( s1 , s2 ) ) ;