text: Minimum number of operations to move all uppercase characters before all lower case characters | 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
code: < ? php function minOperations ( $ str , $ n ) { $ i ; $ lastUpper = -1 ; $ firstLower = -1 ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { if ( ctype_upper ( $ str [ $ i ] ) ) { $ lastUpper = $ i ; break ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( ctype_lower ( $ str [ $ i ] ) ) { $ firstLower = $ i ; break ; } } if ( $ lastUpper == -1 $ firstLower == -1 ) return 0 ; $ countUpper = 0 ; for ( $ i = $ firstLower ; $ i < $ n ; $ i ++ ) { if ( ctype_upper ( $ str [ $ i ] ) ) { $ countUpper ++ ; } } $ countLower = 0 ; for ( $ i = 0 ; $ i < $ lastUpper ; $ i ++ ) { if ( ctype_lower ( $ str [ $ i ] ) ) { $ countLower ++ ; } } return min ( $ countLower , $ countUpper ) ; } { $ str = " geEksFOrGEekS " ; $ n = strlen ( $ str ) ; echo ( minOperations ( $ str , $ n ) ) ; } ? >