Minimum number of operations to move all uppercase characters before all lower case characters
Given a string str, containing upper case and lower case characters. In a single operations, any lowercase character can be converted to an uppercase character and vice versa. The task is to print the minimum number of such operations required so that the resultant string consists of zero or more upper case characters followed by zero or more lower case characters.
Examples:
Input: str = “geEks”
Output: 1
Either the first 2 characters can be converted to uppercase characters i.e. “GEEks” with 2 operations.
Or the third character can be converted to a lowercase character i.e. “geeks” with a single operation.
Input: str = “geek”
Output: 0
The string is already in the specified format.
Approach:
There are two possible cases:
- Find the index of the last uppercase character in the string and convert all the lowercase characters appearing before it into uppercase characters.
- Or, find the index of the first lowercase character in the string and convert all the uppercase characters appearing after it into lowercase characters.
Choose the case where the operations required are minimum.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int minOperations(string str, int n)
{
int 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;
int countUpper = 0;
for (i = firstLower; i < n; i++)
{
if ( isupper (str[i]))
{
countUpper++;
}
}
int countLower = 0;
for (i = 0; i < lastUpper; i++)
{
if ( islower (str[i]))
{
countLower++;
}
}
return min(countLower, countUpper);
}
int main()
{
string str = "geEksFOrGEekS" ;
int n = str.length();
cout << minOperations(str, n) << endl;
}
|
Java
class GFG {
static int minOperations(String str, int n)
{
int i, lastUpper = - 1 , firstLower = - 1 ;
for (i = n - 1 ; i >= 0 ; i--) {
if (Character.isUpperCase(str.charAt(i))) {
lastUpper = i;
break ;
}
}
for (i = 0 ; i < n; i++) {
if (Character.isLowerCase(str.charAt(i))) {
firstLower = i;
break ;
}
}
if (lastUpper == - 1 || firstLower == - 1 )
return 0 ;
int countUpper = 0 ;
for (i = firstLower; i < n; i++) {
if (Character.isUpperCase(str.charAt(i))) {
countUpper++;
}
}
int countLower = 0 ;
for (i = 0 ; i < lastUpper; i++) {
if (Character.isLowerCase(str.charAt(i))) {
countLower++;
}
}
return Math.min(countLower, countUpper);
}
public static void main(String args[])
{
String str = "geEksFOrGEekS" ;
int n = str.length();
System.out.println(minOperations(str, n));
}
}
|
Python 3
def minOperations( str , n):
lastUpper = - 1
firstLower = - 1
for i in range ( n - 1 , - 1 , - 1 ):
if ( str [i].isupper()):
lastUpper = i
break
for i in range (n):
if ( str [i].islower()):
firstLower = i
break
if (lastUpper = = - 1 or firstLower = = - 1 ):
return 0
countUpper = 0
for i in range ( firstLower,n):
if ( str [i].isupper()):
countUpper + = 1
countLower = 0
for i in range (lastUpper):
if ( str [i].islower()):
countLower + = 1
return min (countLower, countUpper)
if __name__ = = "__main__" :
str = "geEksFOrGEekS"
n = len ( str )
print (minOperations( str , n))
|
C#
using System;
class GFG
{
static int minOperations( string str, int n)
{
int i, lastUpper = -1, firstLower = -1;
for (i = n - 1; i >= 0; i--)
{
if (Char.IsUpper(str[i]))
{
lastUpper = i;
break ;
}
}
for (i = 0; i < n; i++)
{
if (Char.IsLower(str[i]))
{
firstLower = i;
break ;
}
}
if (lastUpper == -1 || firstLower == -1)
return 0;
int countUpper = 0;
for (i = firstLower; i < n; i++)
{
if (Char.IsUpper(str[i]))
{
countUpper++;
}
}
int countLower = 0;
for (i = 0; i < lastUpper; i++)
{
if (Char.IsLower(str[i]))
{
countLower++;
}
}
return Math.Min(countLower, countUpper);
}
public static void Main()
{
string str = "geEksFOrGEekS" ;
int n = str.Length;
Console.WriteLine(minOperations(str, n));
}
}
|
PHP
<?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 ));
}
?>
|
Javascript
<script>
function isupper(str) {
return str === str.toUpperCase();
}
function islower(str) {
return str === str.toLowerCase();
}
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 = "geEksFOrGEekS" ;
var n = str.length;
document.write(minOperations(str, n) + "<br>" );
</script>
|
Complexity Analysis:
- Time Complexity: O(N) where N is the length of the string.
- Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
15 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...