Monday, September 23, 2024
Google search engine
HomeData Modelling & AIMinimum moves to make count of lowercase and uppercase letters equal

Minimum moves to make count of lowercase and uppercase letters equal

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: 

Input: S = “AbcdEf”
Output: 
               ABcdEf
Explanation: 
One possible way to modify the string is by converting the second character to S[1]( =’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 <bits/stdc++.h>
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




<script>
    // Javascript program for the above approach
     
    // Function to calculate minimum
    // number of moves required to
    // convert the string
    function minimumTimeToConvertString(S, N)
    {
     
        // Stores Count of upper and
        // lower case characters
        let upper = 0, lower = 0;
                                                                                                                                            
        // Traverse the string S
        for (let i = 0; i < N; i++) {
       
            let c = S[i];
       
            // If current character is
            // uppercase
            if (c == c.toUpperCase()) {
       
                // Increment count
                // of Uppercase characters
                upper++;
            }
            // Otherwise,
            else {
       
                // Increment count
                // of Lowercase characters
                lower++;
            }
        }
       
        // Stores minimum number of moves needed
        let moves = 0;
       
        // If there are more upper
        // case characters
        if (upper > parseInt(N / 2, 10)) {
       
            let i = 0;
            // Iterate until upper is greater
            // than N/2
            while (upper > parseInt(N / 2, 10) && i < N) {
       
                // Convert uppercase into
                // lowercase until upper=N/2
                if (S[i] == S[i].toUpperCase()) {
       
                    S[i] += String.fromCharCode(32);
                    moves++;
                    upper--;
                    lower++;
                }
       
                // Increment the pointer
                i++;
            }
        }
        // If there are more lower
        // case characters
        else if (lower > parseInt(N / 2, 10)) {
       
            let i = 0;
            // Iterate until lower is greater
            // than N/2
            while (lower > parseInt(N / 2, 10) && i < N) {
       
                // Convert lowercase into
                // uppercase until lower=N/2
                if (S[i] == S[i].toLowerCase()) {
       
                    S[i] = String.fromCharCode(S[i].charCodeAt() - 32);
                    moves++;
                    upper++;
                    lower--;
                }
       
                // Increment the pointer
                i++;
            }
        }
       
        // Print moves required
        document.write(moves + "</br>");
       
        // Print resultant string
        document.write(S.join(""));
    }
     
    // Given string
    let S = "AbcdEf";
    let N = S.length;
   
    // Function call
    minimumTimeToConvertString(S.split(''), N);
    
   // This code is contributed by decode2207.
</script>


Output

1
ABcdEf

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

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments