Case | Python3 implementation of the approach ; Function to return the sorted string ; To store the frequencies of the lowercase and the uppercase characters in the given string ; If current character is lowercase then increment its frequency in the lower [ ] array ; Else increment in the upper [ ] array ; Pointers that point to the smallest lowercase and the smallest uppercase characters respectively in the given string ; For every character in the given string ; If the current character is lowercase then replace it with the smallest lowercase character available ; Decrement the frequency of the used character ; Else replace it with the smallest uppercase character available ; Decrement the frequency of the used character ; Return the sorted string ; Driver code
MAX = 26 NEW_LINE def getSortedString ( s , n ) : NEW_LINE INDENT lower = [ 0 ] * MAX ; NEW_LINE upper = [ 0 ] * MAX ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] . islower ( ) ) : NEW_LINE INDENT lower [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE DEDENT elif ( s [ i ] . isupper ( ) ) : NEW_LINE INDENT upper [ ord ( s [ i ] ) - ord ( ' A ' ) ] += 1 ; NEW_LINE DEDENT DEDENT i = 0 ; j = 0 ; NEW_LINE while ( i < MAX and lower [ i ] == 0 ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT while ( j < MAX and upper [ j ] == 0 ) : NEW_LINE INDENT j += 1 ; NEW_LINE DEDENT for k in range ( n ) : NEW_LINE INDENT if ( s [ k ] . islower ( ) ) : NEW_LINE INDENT while ( lower [ i ] == 0 ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT s [ k ] = chr ( i + ord ( ' a ' ) ) ; NEW_LINE lower [ i ] -= 1 ; NEW_LINE DEDENT elif ( s [ k ] . isupper ( ) ) : NEW_LINE INDENT while ( upper [ j ] == 0 ) : NEW_LINE INDENT j += 1 ; NEW_LINE DEDENT s [ k ] = chr ( j + ord ( ' A ' ) ) ; NEW_LINE upper [ j ] -= 1 ; NEW_LINE DEDENT DEDENT return " " . join ( s ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " gEeksfOrgEEkS " ; NEW_LINE n = len ( s ) ; NEW_LINE print ( getSortedString ( list ( s ) , n ) ) ; NEW_LINE DEDENT