Lexicographically largest string possible for a given cost of appending characters | Function to find the lexicographically largest String possible ; If sum is less than 0 ; If sum is equal to 0 ; If sum is less than 0 ; Add current character ; Check if selecting current character generates lexicographically largest String ; Backtrack if solution not found ; Find the lexicographically largest String excluding the current character ; Function to print the lexicographically largest String generated ; Function call ; Stores the String ; Print the lexicographically largest String formed ; Cost of adding each alphabet ; Cost of generating the String
function lexi_largest_String ( a , i , sum , v ) { if ( sum < 0 ) return false ; if ( sum == 0 ) return true ; if ( i < 0 ) return false ; v . push ( i ) ; if ( lexi_largest_String ( a , i , sum - a [ i ] , v ) ) return true ; v . pop ( ) ; return lexi_largest_String ( a , i - 1 , sum , v ) ; } function generateString ( a , sum ) { let v = [ ] ; lexi_largest_String ( a , 25 , sum , v ) ; let s = " " ; for ( let j = 0 ; j < v . length ; j ++ ) s += String . fromCharCode ( v [ j ] + ' ' . charCodeAt ( ) ) ; document . write ( s + " " ) ; } let a = [ 1 , 1 , 2 , 33 , 4 , 6 , 9 , 7 , 36 , 32 , 58 , 32 , 28 , 904 , 22 , 255 , 47 , 69 , 558 , 544 , 21 , 36 , 48 , 85 , 48 , 58 ] ; let sum = 236 ; generateString ( a , sum ) ;