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 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
function minimumTimeToConvertString ( S , N ) { let upper = 0 , lower = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let c = S [ i ] ; if ( c == c . toUpperCase ( ) ) { upper ++ ; } else { lower ++ ; } } let moves = 0 ; if ( upper > parseInt ( N / 2 , 10 ) ) { let i = 0 ; while ( upper > parseInt ( N / 2 , 10 ) && i < N ) { if ( S [ i ] == S [ i ] . toUpperCase ( ) ) { S [ i ] += String . fromCharCode ( 32 ) ; moves ++ ; upper -- ; lower ++ ; } i ++ ; } } else if ( lower > parseInt ( N / 2 , 10 ) ) { let i = 0 ; while ( lower > parseInt ( N / 2 , 10 ) && i < N ) { if ( S [ i ] == S [ i ] . toLowerCase ( ) ) { S [ i ] = String . fromCharCode ( S [ i ] . charCodeAt ( ) - 32 ) ; moves ++ ; upper ++ ; lower -- ; } i ++ ; } } document . write ( moves + " " ) ; document . write ( S . join ( " " ) ) ; } let S = " " ; let N = S . length ; minimumTimeToConvertString ( S . split ( ' ' ) , N ) ;