Lexicographically largest string possible by reversing substrings having even number of 1 s | Function to find the lexicographically maximum string by reversing substrings having even numbers of 1 s ; Store size of string ; Traverse the string ; Count the number of 1 s ; Stores the starting index ; Stores the end index ; Increment count , when 1 is encountered ; Traverse the remaining string ; temp is for Reverse the string from starting and end index ; Printing the string ; Driver Code
def lexicographicallyMax ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT count = 0 NEW_LINE beg = i NEW_LINE end = i NEW_LINE if ( s [ i ] == '1' ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( s [ j ] == '1' ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if ( count % 2 == 0 and count != 0 ) : NEW_LINE INDENT end = j NEW_LINE break NEW_LINE DEDENT DEDENT temp = s [ beg : end + 1 ] NEW_LINE temp = temp [ : : - 1 ] NEW_LINE s = s [ 0 : beg ] + temp + s [ end + 1 : ] NEW_LINE DEDENT print ( s ) NEW_LINE DEDENT S = "0101" NEW_LINE lexicographicallyMax ( S ) NEW_LINE