Lexicographical Maximum substring of string | C # 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
using System ; using System . Collections . Generic ; class GFG { static string LexicographicalMaxString ( string str ) { char maxchar = ' a ' ; List < int > index = new List < int > ( ) ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] >= maxchar ) { maxchar = str [ i ] ; index . Add ( i ) ; } } string maxstring = " " ; for ( int i = 0 ; i < index . Count ; i ++ ) { if ( str . Substring ( index [ i ] ) . CompareTo ( maxstring ) > 0 ) { maxstring = str . Substring ( index [ i ] ) ; } } return maxstring ; } static public void Main ( ) { string str = " acbacbc " ; Console . Write ( LexicographicalMaxString ( str ) ) ; } }