# 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.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the minimum ` `// number of operations required ` `int` `minOperations(string str, ``int` `n) ` `{ ` ` `  `    ``// To store the indices of the last uppercase ` `    ``// and the first lowercase character ` `    ``int` `i, lastUpper = -1, firstLower = -1; ` ` `  `    ``// Find the last uppercase character ` `    ``for` `(i = n - 1; i >= 0; i--) ` `    ``{ ` `        ``if` `(``isupper``(str[i])) ` `        ``{ ` `            ``lastUpper = i; ` `            ``break``; ` `        ``} ` `    ``} ` ` `  `    ``// Find the first lowercase character ` `    ``for` `(i = 0; i < n; i++)  ` `    ``{ ` `        ``if` `(``islower``(str[i]))  ` `        ``{ ` `            ``firstLower = i; ` `            ``break``; ` `        ``} ` `    ``} ` ` `  `    ``// If all the characters are either ` `    ``// uppercase or lowercase ` `    ``if` `(lastUpper == -1 || firstLower == -1) ` `        ``return` `0; ` ` `  `    ``// Count of uppercase characters that appear ` `    ``// after the first lowercase character ` `    ``int` `countUpper = 0; ` `    ``for` `(i = firstLower; i < n; i++)  ` `    ``{ ` `        ``if` `(``isupper``(str[i]))  ` `        ``{ ` `            ``countUpper++; ` `        ``} ` `    ``} ` ` `  `    ``// Count of lowercase characters that appear ` `    ``// before the last uppercase character ` `    ``int` `countLower = 0; ` `    ``for` `(i = 0; i < lastUpper; i++)  ` `    ``{ ` `        ``if` `(``islower``(str[i]))  ` `        ``{ ` `            ``countLower++; ` `        ``} ` `    ``} ` ` `  `    ``// Return the minimum operations required ` `    ``return` `min(countLower, countUpper); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``string str = ``"geEksFOrGEekS"``; ` `    ``int` `n = str.length(); ` `    ``cout << minOperations(str, n) << endl; ` `} ` ` `  `// This code is contributed by ` `// Surendra_Gangwar `

## Java

 `// Java implementation of the approach ` `class` `GFG { ` ` `  `    ``// Function to return the minimum ` `    ``// number of operations required ` `    ``static` `int` `minOperations(String str, ``int` `n) ` `    ``{ ` ` `  `        ``// To store the indices of the last uppercase ` `        ``// and the first lowercase character ` `        ``int` `i, lastUpper = -``1``, firstLower = -``1``; ` ` `  `        ``// Find the last uppercase character ` `        ``for` `(i = n - ``1``; i >= ``0``; i--) { ` `            ``if` `(Character.isUpperCase(str.charAt(i))) { ` `                ``lastUpper = i; ` `                ``break``; ` `            ``} ` `        ``} ` ` `  `        ``// Find the first lowercase character ` `        ``for` `(i = ``0``; i < n; i++) { ` `            ``if` `(Character.isLowerCase(str.charAt(i))) { ` `                ``firstLower = i; ` `                ``break``; ` `            ``} ` `        ``} ` ` `  `        ``// If all the characters are either ` `        ``// uppercase or lowercase ` `        ``if` `(lastUpper == -``1` `|| firstLower == -``1``) ` `            ``return` `0``; ` ` `  `        ``// Count of uppercase characters that appear ` `        ``// after the first lowercase character ` `        ``int` `countUpper = ``0``; ` `        ``for` `(i = firstLower; i < n; i++) { ` `            ``if` `(Character.isUpperCase(str.charAt(i))) { ` `                ``countUpper++; ` `            ``} ` `        ``} ` ` `  `        ``// Count of lowercase characters that appear ` `        ``// before the last uppercase character ` `        ``int` `countLower = ``0``; ` `        ``for` `(i = ``0``; i < lastUpper; i++) { ` `            ``if` `(Character.isLowerCase(str.charAt(i))) { ` `                ``countLower++; ` `            ``} ` `        ``} ` ` `  `        ``// Return the minimum operations required ` `        ``return` `Math.min(countLower, countUpper); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``String str = ``"geEksFOrGEekS"``; ` `        ``int` `n = str.length(); ` `        ``System.out.println(minOperations(str, n)); ` `    ``} ` `} `

## Python 3

 `# Python 3 implementation of the approach ` ` `  `# Function to return the minimum ` `# number of operations required ` `def` `minOperations(``str``, n): ` ` `  `    ``# To store the indices of the last uppercase ` `    ``# and the first lowercase character ` `    ``lastUpper ``=` `-``1` `    ``firstLower ``=` `-``1` ` `  `    ``# Find the last uppercase character ` `    ``for` `i ``in` `range``( n ``-` `1``, ``-``1``, ``-``1``): ` `        ``if` `(``str``[i].isupper()): ` `            ``lastUpper ``=` `i ` `            ``break` ` `  `    ``# Find the first lowercase character ` `    ``for` `i ``in` `range``(n): ` `        ``if` `(``str``[i].islower()): ` `            ``firstLower ``=` `i ` `            ``break` ` `  `    ``# If all the characters are either ` `    ``# uppercase or lowercase ` `    ``if` `(lastUpper ``=``=` `-``1` `or` `firstLower ``=``=` `-``1``): ` `        ``return` `0` ` `  `    ``# Count of uppercase characters that appear ` `    ``# after the first lowercase character ` `    ``countUpper ``=` `0` `    ``for` `i ``in` `range``( firstLower,n): ` `        ``if` `(``str``[i].isupper()): ` `            ``countUpper ``+``=` `1` ` `  `    ``# Count of lowercase characters that appear ` `    ``# before the last uppercase character ` `    ``countLower ``=` `0` `    ``for` `i ``in` `range``(lastUpper): ` `        ``if` `(``str``[i].islower()): ` `            ``countLower ``+``=` `1` ` `  `    ``# Return the minimum operations required ` `    ``return` `min``(countLower, countUpper) ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``str` `=` `"geEksFOrGEekS"` `    ``n ``=` `len``(``str``) ` `    ``print``(minOperations(``str``, n)) ` ` `  `# This code is contributed by Ita_c. `

## C#

 `// C# implementation of the approach  ` `using` `System; ` ` `  `class` `GFG ` `{  ` ` `  `    ``// Function to return the minimum  ` `    ``// number of operations required  ` `    ``static` `int` `minOperations(``string` `str, ``int` `n)  ` `    ``{  ` ` `  `        ``// To store the indices of the last uppercase  ` `        ``// and the first lowercase character  ` `        ``int` `i, lastUpper = -1, firstLower = -1;  ` ` `  `        ``// Find the last uppercase character  ` `        ``for` `(i = n - 1; i >= 0; i--)  ` `        ``{  ` `            ``if` `(Char.IsUpper(str[i]))  ` `            ``{  ` `                ``lastUpper = i;  ` `                ``break``;  ` `            ``}  ` `        ``}  ` ` `  `        ``// Find the first lowercase character  ` `        ``for` `(i = 0; i < n; i++) ` `        ``{  ` `            ``if` `(Char.IsLower(str[i]))  ` `            ``{  ` `                ``firstLower = i;  ` `                ``break``;  ` `            ``}  ` `        ``}  ` ` `  `        ``// If all the characters are either  ` `        ``// uppercase or lowercase  ` `        ``if` `(lastUpper == -1 || firstLower == -1)  ` `            ``return` `0;  ` ` `  `        ``// Count of uppercase characters that appear  ` `        ``// after the first lowercase character  ` `        ``int` `countUpper = 0;  ` `        ``for` `(i = firstLower; i < n; i++) ` `        ``{  ` `            ``if` `(Char.IsUpper(str[i])) ` `            ``{  ` `                ``countUpper++;  ` `            ``}  ` `        ``}  ` ` `  `        ``// Count of lowercase characters that appear  ` `        ``// before the last uppercase character  ` `        ``int` `countLower = 0;  ` `        ``for` `(i = 0; i < lastUpper; i++) ` `        ``{  ` `            ``if` `(Char.IsLower(str[i]))  ` `            ``{  ` `                ``countLower++;  ` `            ``}  ` `        ``}  ` ` `  `        ``// Return the minimum operations required  ` `        ``return` `Math.Min(countLower, countUpper);  ` `    ``}  ` ` `  `    ``// Driver Code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `        ``string` `str = ``"geEksFOrGEekS"``;  ` `        ``int` `n = str.Length;  ` `        ``Console.WriteLine(minOperations(str, n));  ` `    ``}  ` `}  ` ` `  `// This code is contributed by Ryuga `

## PHP

 `= 0; ``\$i``--) ` `    ``{ ` `        ``if` `(ctype_upper(``\$str``[``\$i``])) ` `        ``{ ` `            ``\$lastUpper` `= ``\$i``; ` `            ``break``; ` `        ``} ` `    ``} ` ` `  `    ``// Find the first lowercase character ` `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$n``; ``\$i``++)  ` `    ``{ ` `        ``if` `(ctype_lower(``\$str``[``\$i``]))  ` `        ``{ ` `            ``\$firstLower` `= ``\$i``; ` `            ``break``; ` `        ``} ` `    ``} ` ` `  `    ``// If all the characters are either ` `    ``// uppercase or lowercase ` `    ``if` `(``\$lastUpper` `== -1 || ``\$firstLower` `== -1) ` `        ``return` `0; ` ` `  `    ``// Count of uppercase characters that appear ` `    ``// after the first lowercase character ` `    ``\$countUpper` `= 0; ` `    ``for` `(``\$i` `= ``\$firstLower``; ``\$i` `< ``\$n``; ``\$i``++)  ` `    ``{ ` `        ``if` `(ctype_upper(``\$str``[``\$i``]))  ` `        ``{ ` `            ``\$countUpper``++; ` `        ``} ` `    ``} ` ` `  `    ``// Count of lowercase characters that appear ` `    ``// before the last uppercase character ` `    ``\$countLower` `= 0; ` `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$lastUpper``; ``\$i``++) ` `    ``{ ` `        ``if` `(ctype_lower(``\$str``[``\$i``]))  ` `        ``{ ` `            ``\$countLower``++; ` `        ``} ` `    ``} ` ` `  `    ``// Return the minimum operations required ` `    ``return` `min(``\$countLower``, ``\$countUpper``); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``{ ` `        ``\$str` `= ``"geEksFOrGEekS"``; ` `        ``\$n` `= ``strlen``(``\$str``); ` `        ``echo``(minOperations(``\$str``, ``\$n``)); ` `    ``} ` ` `  `// This code is contributed by Code_Mech ` `?> `

Output:

```6
```

Time Complexity: O(N) where N is the length of the string.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6173623,"math_prob":0.9490125,"size":9247,"snap":"2020-10-2020-16","text_gpt3_token_len":2476,"char_repetition_ratio":0.22211403,"word_repetition_ratio":0.39500913,"special_character_ratio":0.289283,"punctuation_ratio":0.12622629,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9969408,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-03-30T20:37:52Z\",\"WARC-Record-ID\":\"<urn:uuid:edc639ee-1e59-4122-aac7-9bd13805427f>\",\"Content-Length\":\"195931\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8246902e-2746-4b5f-965c-89495daf4add>\",\"WARC-Concurrent-To\":\"<urn:uuid:5a0d182e-d604-4027-b8ce-db08416ad342>\",\"WARC-IP-Address\":\"23.221.227.170\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/minimum-number-of-operations-to-move-all-uppercase-characters-before-all-lower-case-characters/\",\"WARC-Payload-Digest\":\"sha1:ZRBRV44RB6GPMHFIFE6K2D67Q5AFDUP3\",\"WARC-Block-Digest\":\"sha1:UMN6X4BXOF52G43V6WVXKZSMAY77HT56\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-16/CC-MAIN-2020-16_segments_1585370497301.29_warc_CC-MAIN-20200330181842-20200330211842-00049.warc.gz\"}"}