📌  相关文章
📜  使小写和大写字母数量相等的最小移动

📅  最后修改于: 2022-05-13 01:57:07.113000             🧑  作者: Mango

使小写和大写字母数量相等的最小移动

给定一个长度为N的字符串S ,任务是找到将字符串转换为由一半大写和一半小写字符组成的字符串所需的最小移动次数。

例子:

方法:这个想法是只转换那些在任何情况下都是多余的字符。请按照以下步骤解决问题:

  • 找到大写英文字母和小写英文字母的计数并将它们存储在变量中,分别说大写小写
  • 初始化一个变量,比如将移动设置为0,以存储修改字符串的最小移动次数。
  • 如果大写字符更多,则遍历字符串并将大写字符转换为小写字符,直到两个大小写字符数量相等。
  • 否则,如果小写字符较多,则遍历字符串S ,将小写字符转换为大写字符,直到大小写字符数量相等。
  • 最后,完成上述步骤后,将moves的值打印为答案,将字符串S打印为修改后的字符串。

下面是上述方法的实现:

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


输出
1
ABcdEf

时间复杂度: O(N)
辅助空间: O(1)