Lexicographical Maximum substring of string | Python 3 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
def LexicographicalMaxString ( st ) : NEW_LINE INDENT maxchar = ' a ' NEW_LINE index = [ ] NEW_LINE for i in range ( len ( st ) ) : NEW_LINE INDENT if ( st [ i ] >= maxchar ) : NEW_LINE INDENT maxchar = st [ i ] NEW_LINE index . append ( i ) NEW_LINE DEDENT DEDENT maxstring = " " NEW_LINE for i in range ( len ( index ) ) : NEW_LINE INDENT if ( st [ index [ i ] : len ( st ) ] > maxstring ) : NEW_LINE INDENT maxstring = st [ index [ i ] : len ( st ) ] NEW_LINE DEDENT DEDENT return maxstring NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " acbacbc " NEW_LINE print ( LexicographicalMaxString ( st ) ) NEW_LINE DEDENT