Lexicographically largest string possible by repeatedly appending first character of two given strings | C # program for the above approach ; Function to make the lexicographically largest string by merging two strings ; Stores the resultant string ; If the string word1 is lexographically greater than or equal to word2 ; Update the string merge ; Erase the first index of the string word1 ; Otherwise ; Update the string merge ; Erase the first index of the string word2 ; Return the final string ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static string largestMerge ( string word1 , string word2 ) { string merge = " " ; while ( word1 . Length != 0 word2 . Length != 0 ) { if ( String . Compare ( word1 , word2 ) == 0 || String . Compare ( word1 , word2 ) > 0 ) { merge = merge + word1 [ 0 ] ; word1 = word1 . Substring ( 1 ) ; } else { merge = merge + word2 [ 0 ] ; word2 = word2 . Substring ( 1 ) ; } } return merge ; } public static void Main ( ) { string S1 = " xyzxyz " ; string S2 = " xywzxyx " ; Console . Write ( largestMerge ( S1 , S2 ) ) ; } }