Minimum number of operations to move all uppercase characters before all lower case characters | JavaScript implementation of the approach Check UpperCase ; Function to return the minimum number of operations required ; To store the indices of the last uppercase and the first lowercase character ; Find the last uppercase character ; Find the first lowercase character ; If all the characters are either uppercase or lowercase ; Count of uppercase characters that appear after the first lowercase character ; Count of lowercase characters that appear before the last uppercase character ; Return the minimum operations required ; Driver Code
function isupper ( str ) { return str === str . toUpperCase ( ) ; } function minOperations ( str , n ) { var i , lastUpper = - 1 , firstLower = - 1 ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( isupper ( str [ i ] ) ) { lastUpper = i ; break ; } } for ( i = 0 ; i < n ; i ++ ) { if ( islower ( str [ i ] ) ) { firstLower = i ; break ; } } if ( lastUpper === - 1 firstLower === - 1 ) return 0 ; var countUpper = 0 ; for ( i = firstLower ; i < n ; i ++ ) { if ( isupper ( str [ i ] ) ) { countUpper ++ ; } } var countLower = 0 ; for ( i = 0 ; i < lastUpper ; i ++ ) { if ( islower ( str [ i ] ) ) { countLower ++ ; } } return Math . min ( countLower , countUpper ) ; } var str = " " ; var n = str . length ; document . write ( minOperations ( str , n ) + " " ) ;