Minimum moves to make count of lowercase and uppercase letters equal | C # program for the above approach ; Function to calculate minimum number of moves required to convert the string ; Stores Count of upper and lower case characters ; Traverse the string 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 ; Given string ; Function call
using System ; class GFG { static void minimumTimeToConvertString ( char [ ] S , int N ) { int upper = 0 , lower = 0 ; for ( int i = 0 ; i < N ; i ++ ) { char c = S [ i ] ; if ( Char . IsUpper ( c ) ) { upper ++ ; } else { lower ++ ; } } int moves = 0 ; if ( upper > N / 2 ) { int i = 0 ; while ( upper > N / 2 && i < N ) { if ( Char . IsUpper ( S [ i ] ) ) { S [ i ] += ( char ) 32 ; moves ++ ; upper -- ; lower ++ ; } i ++ ; } } else if ( lower > N / 2 ) { int i = 0 ; while ( lower > N / 2 && i < N ) { if ( Char . IsLower ( S [ i ] ) ) { S [ i ] = ( char ) ( ( int ) S [ i ] - 32 ) ; moves ++ ; upper ++ ; lower -- ; } i ++ ; } } Console . WriteLine ( moves ) ; Console . WriteLine ( new string ( S ) ) ; } static void Main ( ) { string S = " AbcdEf " ; int N = S . Length ; minimumTimeToConvertString ( S . ToCharArray ( ) , N ) ; } }