a | Function to return the modified string ; To store the previous character ; To store the starting indices of all the groups ; Starting index of the first group ; If the current character and the previous character differ ; Push the starting index of the new group ; The current character now becomes previous ; Sort the first group ; Sort all the remaining groups ; print ( ''.join(st)) ; Return the modified string ; Driver code
def get_string ( st , n ) : NEW_LINE INDENT prev = st [ 0 ] NEW_LINE result = [ ] NEW_LINE result . append ( 0 ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( st [ i ] . isdigit ( ) != prev . isdigit ( ) ) : NEW_LINE INDENT result . append ( i ) NEW_LINE DEDENT prev = st [ i ] NEW_LINE DEDENT st = list ( st ) NEW_LINE st [ : result [ 0 ] ] . sort ( ) NEW_LINE p = st . copy ( ) NEW_LINE for i in range ( len ( result ) - 1 ) : NEW_LINE INDENT p [ result [ i ] : result [ i + 1 ] ] = sorted ( st [ result [ i ] : result [ i + 1 ] ] ) NEW_LINE DEDENT p [ len ( p ) - 2 : ] = sorted ( st [ result [ - 1 ] : ] ) NEW_LINE return ' ' . join ( p ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = "121geeks21" NEW_LINE n = len ( st ) NEW_LINE print ( get_string ( st , n ) ) NEW_LINE DEDENT