Minimum moves to make count of lowercase and uppercase letters equal | Function to calculate minimum number of moves required to convert the string ; Stores Count of upper and lower case characters ; Traverse the S ; If current character is uppercase ; Increment count of Uppercase characters ; Otherwise , ; Increment count of Lowercase characters ; Stores minimum number of moves needed ; If there are more upper case characters ; Iterate until upper is greater than N / 2 ; Convert uppercase into lowercase until upper = N / 2 ; Increment the pointer ; If there are more lower case characters ; Iterate until lower is greater than N / 2 ; Convert lowercase into uppercase until lower = N / 2 ; Increment the pointer ; Print moves required ; Print resultant string ; Driver Code ; Given string ; Function call
def minimumTimeToConvertString ( S , N ) : NEW_LINE INDENT S = [ i for i in S ] NEW_LINE upper = 0 NEW_LINE lower = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT c = S [ i ] NEW_LINE if ( c . isupper ( ) ) : NEW_LINE INDENT upper += 1 NEW_LINE DEDENT else : NEW_LINE INDENT lower += 1 NEW_LINE DEDENT DEDENT moves = 0 NEW_LINE if ( upper > N // 2 ) : NEW_LINE INDENT i = 0 NEW_LINE while ( upper > N // 2 and i < N ) : NEW_LINE INDENT if ( S [ i ] . isupper ( ) ) : NEW_LINE INDENT S [ i ] += 32 NEW_LINE moves += 1 NEW_LINE upper -= 1 NEW_LINE lower += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT elif ( lower > N // 2 ) : NEW_LINE INDENT i = 0 NEW_LINE while ( lower > N // 2 and i < N ) : NEW_LINE INDENT if ( S [ i ] . islower ( ) ) : NEW_LINE INDENT S [ i ] = chr ( ord ( S [ i ] ) - 32 ) NEW_LINE moves += 1 NEW_LINE upper += 1 NEW_LINE lower -= 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT print ( moves ) NEW_LINE print ( " " . join ( S ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " AbcdEf " NEW_LINE N = len ( S ) NEW_LINE minimumTimeToConvertString ( S , N ) NEW_LINE DEDENT