Alternate Lower Upper String Sort | Python3 program for unusul string sorting ; Function for alternate sorting of string ; Count occurrences of individual lower case and upper case characters ; Traverse through count arrays and one by one pick characters . Below loop takes O ( n ) time considering the MAX is constant . ; Driver code
MAX = 26 NEW_LINE def alternateSort ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE lCount = [ 0 for i in range ( MAX ) ] NEW_LINE uCount = [ 0 for i in range ( MAX ) ] NEW_LINE s = list ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] . isupper ( ) ) : NEW_LINE INDENT uCount [ ord ( s [ i ] ) - ord ( ' A ' ) ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT lCount [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT DEDENT i = 0 NEW_LINE j = 0 NEW_LINE k = 0 NEW_LINE while ( k < n ) : NEW_LINE INDENT while ( i < MAX and uCount [ i ] == 0 ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT if ( i < MAX ) : NEW_LINE INDENT s [ k ] = chr ( ord ( ' A ' ) + i ) NEW_LINE k += 1 NEW_LINE uCount [ i ] -= 1 NEW_LINE DEDENT while ( j < MAX and lCount [ j ] == 0 ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT if ( j < MAX ) : NEW_LINE INDENT s [ k ] = chr ( ord ( ' a ' ) + j ) NEW_LINE k += 1 NEW_LINE lCount [ j ] -= 1 NEW_LINE DEDENT DEDENT print ( " " . join ( s ) ) NEW_LINE DEDENT str = " bAwutndekWEdkd " NEW_LINE alternateSort ( str ) NEW_LINE