📌  相关文章
📜  通过将元音更改为辅音来最小化更改以使所有字符相等,反之亦然

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

通过将元音更改为辅音来最小化更改以使所有字符相等,反之亦然

给定一个由小写字符组成的字符串str[] ,任务是使字符串的所有字符在最少的操作数中相等,以便在每个操作中选择一个元音并将其更改为辅音,反之亦然。

例子:

方法:从这个问题可以推导出辅音转元音需要1个操作,元音转元音或辅音转辅音需要2个步骤如果将元音变为元音或将辅音变为元音,然后再将辅音变为辅音,则将元音变为辅音,然后再返回元音。这个想法是保持两个变量 -

  • Cv = 将所有字符更改为最大出现元音的成本 = 否。辅音数+(元音总数-最大元音出现的频率)* 2
  • 抄送= 将所有字符更改为最大出现辅音的成本 = 否。元音+(辅音总数-最大出现辅音的频率)* 2

现在这两个中的最小值,即min(Cv, Cc)将给出我们可以转换字符串所需的最小步数。请按照以下步骤解决问题:

  • 将变量ans、元音辅音初始化为0以存储答案、元音个数和辅音个数。
  • 将 2 个变量max_vowelsmax_consonants初始化为INT_MIN以找到给定字符串中出现的最大元音和出现的最大辅音。
  • 初始化 2 个 unordered_map freq_vowels[]freq_consonant[]存储元音和辅音的频率。
  • 使用变量i迭代范围[0, N)并执行以下步骤:
    • 如果当前字符是元音,则将元音的数量增加1 ,并将其在地图中的频率增加1 ,否则对辅音执行此操作。
  • 遍历 unordered_maps 并找到出现的最大元音和辅音。
  • 使用上面的公式,计算ans。
  • 执行上述步骤后,打印ans的值作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum number
// of steps to make all characters equal
int operations(string s)
{
 
    // Initializing the variables
    int ans = 0;
    int vowels = 0, consonants = 0;
    int max_vowels = INT_MIN;
    int max_consonants = INT_MIN;
 
    // Store the frequency
    unordered_map freq_consonants;
    unordered_map freq_vowels;
 
    // Iterate over the string
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == 'a' or s[i] == 'e' or s[i] == 'i'
            or s[i] == 'o' or s[i] == 'u') {
 
            // Calculate the total
            // number of vowels
            vowels += 1;
 
            // Storing frequency of
            // each vowel
            freq_vowels[s[i]] += 1;
        }
        else {
 
            // Count the consonants
            consonants += 1;
 
            // Storing the frequency of
            // each consonant
            freq_consonants[s[i]] += 1;
        }
    }
 
    // Iterate over the 2 maps
    for (auto it = freq_consonants.begin();
         it != freq_consonants.end(); it++) {
 
        // Maximum frequency of consonant
        max_consonants = max(
            max_consonants,
            it->second);
    }
    for (auto it = freq_vowels.begin();
         it != freq_vowels.end(); it++) {
 
        // Maximum frequency of vowel
        max_vowels
            = max(max_vowels,
                  it->second);
    }
 
    // Find the result
    ans = min((2 * (vowels - max_vowels)
               + consonants),
              (2 * (consonants - max_vowels)
               + consonants));
 
    return ans;
}
 
// Driver Code
int main()
{
    string S = "geeksforgeeks";
    cout << operations(S);
 
    return 0;
}


Python3
# Python 3 program for the above approach
import sys
from collections import defaultdict
 
# Function to find the minimum number
# of steps to make all characters equal
def operations(s):
 
    # Initializing the variables
    ans = 0
    vowels = 0
    consonants = 0
    max_vowels = -sys.maxsize-1
    max_consonants = -sys.maxsize-1
 
    # Store the frequency
    freq_consonants = defaultdict(int)
    freq_vowels = defaultdict(int)
 
    # Iterate over the string
    for i in range(len(s)):
        if (s[i] == 'a' or s[i] == 'e' or s[i] == 'i'
                or s[i] == 'o' or s[i] == 'u'):
 
            # Calculate the total
            # number of vowels
            vowels += 1
 
            # Storing frequency of
            # each vowel
            freq_vowels[s[i]] += 1
 
        else:
 
            # Count the consonants
            consonants += 1
 
            # Storing the frequency of
            # each consonant
            freq_consonants[s[i]] += 1
 
    # Iterate over the 2 maps
    for it in freq_consonants:
 
        # Maximum frequency of consonant
        max_consonants = max(
            max_consonants,
            freq_consonants[it])
 
    for it in freq_vowels:
 
        # Maximum frequency of vowel
        max_vowels = max(max_vowels,
                         freq_vowels[it])
 
    # Find the result
    ans = min((2 * (vowels - max_vowels)
               + consonants),
              (2 * (consonants - max_vowels)
               + consonants))
 
    return ans
 
# Driver Code
if __name__ == "__main__":
 
    S = "geeksforgeeks"
    print(operations(S))
 
    # This code is contributed by ukasp.


Javascript


输出:
10

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