Lexicographical Maximum substring of string | Javascript program to find the lexicographically maximum substring . ; We store all the indexes of maximum characters we have in the string ; We form a substring from that maximum character index till end and check if its greater that maxstring ; Driver code
function LexicographicalMaxString ( str ) { let maxchar = ' ' ; let index = [ ] ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] >= maxchar ) { maxchar = str [ i ] ; index . push ( i ) ; } } let maxstring = " " ; for ( let i = 0 ; i < index . length ; i ++ ) { if ( str . substring ( index [ i ] , str . length ) > maxstring ) { maxstring = str . substring ( index [ i ] , str . length ) ; } } return maxstring ; } let str = " " ; document . write ( LexicographicalMaxString ( str ) ) ;