Related Articles

# Minimum moves to make count of lowercase and uppercase letters equal

• Last Updated : 10 Aug, 2021

Given a string, S of even length N, the task is to find the minimum number of moves needed to convert the string to a string consisting of half upper case and half lower case characters.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: S = “AbcdEf”
Output:
ABcdEf
Explanation:
One possible way to modify the string is by converting the second character to S( =’b’) to an uppercase character. Thereafter, the string modifies to “ABcdEf” which consists of 3 lower case and upper case characters.

Input: S = “ABCdef”
Output:  0
ABCdef

Approach: The idea is to convert only those characters that are extra in any of the cases. Follow the steps below to solve the problem:

• Find the count of the upper case English alphabet and lower case English alphabet and store them in variables, say upper and lower respectively.
• Initialize a variable, say moves as 0, to store the minimum number of moves to modify the string.
• If upper case characters are more, then traverse the string and convert upper case characters into the lower case until both cases characters are equal in number.
• Otherwise, if the lower case characters are more, traverse the string S and convert the lower case characters into the upper case until both cases characters are equal in number.
• Finally, after completing the above steps, print the value of moves as the answer and string S as the modified string.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to calculate minimum``// number of moves required to``// convert the string``void` `minimumTimeToConvertString(string S, ``int` `N)``{``    ``// Stores Count of upper and``    ``// lower case characters``    ``int` `upper = 0, lower = 0;``                                                                                                                                     ` `    ``// Traverse the string S``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``char` `c = S[i];` `        ``// If current character is``        ``// uppercase``        ``if` `(``isupper``(c)) {` `            ``// Increment count``            ``// of Uppercase characters``            ``upper++;``        ``}``        ``// Otherwise,``        ``else` `{` `            ``// Increment count``            ``// of Lowercase characters``            ``lower++;``        ``}``    ``}` `    ``// Stores minimum number of moves needed``    ``int` `moves = 0;` `    ``// If there are more upper``    ``// case characters``    ``if` `(upper > N / 2) {` `        ``int` `i = 0;``        ``// Iterate until upper is greater``        ``// than N/2``        ``while` `(upper > N / 2 && i < N) {` `            ``// Convert uppercase into``            ``// lowercase until upper=N/2``            ``if` `(``isupper``(S[i])) {` `                ``S[i] += 32;``                ``moves++;``                ``upper--;``                ``lower++;``            ``}` `            ``// Increment the pointer``            ``i++;``        ``}``    ``}``    ``// If there are more lower``    ``// case characters``    ``else` `if` `(lower > N / 2) {` `        ``int` `i = 0;``        ``// Iterate until lower is greater``        ``// than N/2``        ``while` `(lower > N / 2 && i < N) {` `            ``// Convert lowercase into``            ``// uppercase until lower=N/2``            ``if` `(``islower``(S[i])) {` `                ``S[i] -= 32;``                ``moves++;``                ``upper++;``                ``lower--;``            ``}` `            ``// Increment the pointer``            ``i++;``        ``}``    ``}` `    ``// Print moves required``    ``cout << moves << endl;` `    ``// Print resultant string``    ``cout << S << endl;``}` `// Driver Code``int` `main()``{``    ``// Given string``    ``string S = ``"AbcdEf"``;``    ``int` `N = S.length();` `    ``// Function call``    ``minimumTimeToConvertString(S, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``class` `GFG{` `// Function to calculate minimum``// number of moves required to``// convert the string``static` `void` `minimumTimeToConvertString(String S, ``int` `N)``{``    ` `    ``// Stores Count of upper and``    ``// lower case characters``    ``int` `upper = ``0``, lower = ``0``;` `    ``// Traverse the string S``    ``for``(``int` `i = ``0``; i < N; i++)``    ``{``        ``char` `c = S.charAt(i);` `        ``// If current character is``        ``// uppercase``        ``if` `(Character.isUpperCase(c))``        ``{``            ` `            ``// Increment count``            ``// of Uppercase characters``            ``upper++;``        ``}``        ` `        ``// Otherwise,``        ``else``        ``{``            ` `            ``// Increment count``            ``// of Lowercase characters``            ``lower++;``        ``}``    ``}` `    ``// Stores minimum number of moves needed``    ``int` `moves = ``0``;` `    ``// If there are more upper``    ``// case characters``    ``if` `(upper > N / ``2``)``    ``{``        ``int` `i = ``0``;``        ` `        ``// Iterate until upper is greater``        ``// than N/2``        ``while` `(upper > N / ``2` `&& i < N)``        ``{``            ` `            ``// Convert uppercase into``            ``// lowercase until upper=N/2``            ``if` `(Character.isUpperCase(S.charAt(i)))``            ``{``                ``S = S.substring(``0``, i) +``                   ``(``char``)(S.charAt(i) + ``32``) +``                        ``S.substring(i + ``1``);``                ``moves++;``                ``upper--;``                ``lower++;``            ``}` `            ``// Increment the pointer``            ``i++;``        ``}``    ``}``    ` `    ``// If there are more lower``    ``// case characters``    ``else` `if` `(lower > N / ``2``)``    ``{``        ``int` `i = ``0``;``        ` `        ``// Iterate until lower is greater``        ``// than N/2``        ``while` `(lower > N / ``2` `&& i < N)``        ``{``            ` `            ``// Convert lowercase into``            ``// uppercase until lower=N/2``            ``if` `(Character.isLowerCase(S.charAt(i)))``            ``{``                ``S = S.substring(``0``, i) +``                   ``(``char``)(S.charAt(i) - ``32``) +``                        ``S.substring(i + ``1``);``                        ` `                ``moves++;``                ``upper++;``                ``lower--;``            ``}` `            ``// Increment the pointer``            ``i++;``        ``}``    ``}` `    ``// Print moves required``    ``System.out.println(moves);` `    ``// Print resultant string``    ``System.out.println(S);``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ` `    ``// Given string``    ``String S = ``"AbcdEf"``;``    ``int` `N = S.length();` `    ``// Function call``    ``minimumTimeToConvertString(S, N);``}``}` `// This code is contributed by abhinavjain194`

## Python3

 `# Python3 program for the above approach` `# Function to calculate minimum``# number of moves required to``# convert the string``def` `minimumTimeToConvertString(S, N):``    ` `    ``S ``=` `[i ``for` `i ``in` `S]``    ` `    ``# Stores Count of upper and``    ``# lower case characters``    ``upper ``=` `0``    ``lower ``=` `0` `    ``# Traverse the S``    ``for` `i ``in` `range``(N):``        ``c ``=` `S[i]` `        ``# If current character is``        ``# uppercase``        ``if` `(c.isupper()):` `            ``# Increment count``            ``# of Uppercase characters``            ``upper ``+``=` `1``            ` `        ``# Otherwise,``        ``else``:` `            ``# Increment count``            ``# of Lowercase characters``            ``lower ``+``=` `1` `    ``# Stores minimum number of moves needed``    ``moves ``=` `0` `    ``# If there are more upper``    ``# case characters``    ``if` `(upper > N ``/``/` `2``):``        ``i ``=` `0``        ` `        ``# Iterate until upper is greater``        ``# than N/2``        ``while` `(upper > N ``/``/` `2` `and` `i < N):` `            ``# Convert uppercase into``            ``# lowercase until upper=N/2``            ``if` `(S[i].isupper()):``                ``S[i] ``+``=` `32``                ``moves ``+``=` `1``                ``upper ``-``=` `1``                ``lower ``+``=` `1` `            ``# Increment the pointer``            ``i ``+``=` `1``            ` `    ``# If there are more lower``    ``# case characters``    ``elif` `(lower > N ``/``/` `2``):``        ``i ``=` `0``        ` `        ``# Iterate until lower is greater``        ``# than N/2``        ``while` `(lower > N ``/``/` `2` `and` `i < N):` `            ``# Convert lowercase into``            ``# uppercase until lower=N/2``            ``if` `(S[i].islower()):``                ``S[i] ``=` `chr``(``ord``(S[i]) ``-` `32``)``                ``moves ``+``=` `1``                ``upper ``+``=` `1``                ``lower ``-``=` `1` `            ``# Increment the pointer``            ``i ``+``=` `1` `    ``# Print moves required``    ``print``(moves)` `    ``# Print resultant string``    ``print``("".join(S))` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``# Given string``    ``S ``=` `"AbcdEf"``    ``N ``=` `len``(S)` `    ``# Function call``    ``minimumTimeToConvertString(S, N)` `# This code is contributed by mohit kumar 29`

## C#

 `// C# program for the above approach``using` `System;``class` `GFG {``    ` `    ``// Function to calculate minimum``    ``// number of moves required to``    ``// convert the string``    ``static` `void` `minimumTimeToConvertString(``char``[] S, ``int` `N)``    ``{``        ``// Stores Count of upper and``        ``// lower case characters``        ``int` `upper = 0, lower = 0;``                                                                                                                                          ` `        ``// Traverse the string S``        ``for` `(``int` `i = 0; i < N; i++) {``     ` `            ``char` `c = S[i];``     ` `            ``// If current character is``            ``// uppercase``            ``if` `(Char.IsUpper(c)) {``     ` `                ``// Increment count``                ``// of Uppercase characters``                ``upper++;``            ``}``            ``// Otherwise,``            ``else` `{``     ` `                ``// Increment count``                ``// of Lowercase characters``                ``lower++;``            ``}``        ``}``     ` `        ``// Stores minimum number of moves needed``        ``int` `moves = 0;``     ` `        ``// If there are more upper``        ``// case characters``        ``if` `(upper > N / 2) {``     ` `            ``int` `i = 0;``            ``// Iterate until upper is greater``            ``// than N/2``            ``while` `(upper > N / 2 && i < N) {``     ` `                ``// Convert uppercase into``                ``// lowercase until upper=N/2``                ``if` `(Char.IsUpper(S[i])) {``     ` `                    ``S[i] += (``char``)32;``                    ``moves++;``                    ``upper--;``                    ``lower++;``                ``}``     ` `                ``// Increment the pointer``                ``i++;``            ``}``        ``}``        ``// If there are more lower``        ``// case characters``        ``else` `if` `(lower > N / 2) {``     ` `            ``int` `i = 0;``            ``// Iterate until lower is greater``            ``// than N/2``            ``while` `(lower > N / 2 && i < N) {``     ` `                ``// Convert lowercase into``                ``// uppercase until lower=N/2``                ``if` `(Char.IsLower(S[i])) {``     ` `                    ``S[i] = (``char``)((``int``)S[i] - 32);``                    ``moves++;``                    ``upper++;``                    ``lower--;``                ``}``     ` `                ``// Increment the pointer``                ``i++;``            ``}``        ``}``     ` `        ``// Print moves required``        ``Console.WriteLine(moves);``     ` `        ``// Print resultant string``        ``Console.WriteLine(``new` `string``(S));``    ``}` `  ``static` `void` `Main() {``    ``// Given string``    ``string` `S = ``"AbcdEf"``;``    ``int` `N = S.Length;`` ` `    ``// Function call``    ``minimumTimeToConvertString(S.ToCharArray(), N);``  ``}``}` `// This code is contributed by mukesh07.`

## Javascript

 ``
Output
```1
ABcdEf```

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up