通过将元音更改为辅音,使给定字符串中的所有字符相等,反之亦然
给定一个包含小写字符的字符串str ,任务是找到使所有字符相等所需的最小操作数。在一次操作中,任何辅音都可以转换为任何元音,或者任何元音都可以转换为任何辅音。
例子:
Input: str = “banana”
Output: 3
Explanation: Convert all consonants to Vowel character A
Input: str = “hexon”
Output: 5
Explanation: Convert E to O first then all the consonants to Alphabet O
方法:这里的基本问题是:
- 将辅音变为另一个辅音或将元音变为另一个元音所需的操作: 2 操作
- 将辅音变为元音或将元音变为辅音所需的操作: 1 操作
因此,很明显,将辅音变为元音或将元音变为辅音比将辅音变为另一个辅音或将元音变为另一个元音更经济。
现在要解决这个问题,请按照以下步骤操作:
- 计算所有字符的频率,找到频率最高的辅音和元音,分别假设A和B。
- 尝试将所有字符更改为A和B ,并将所需的操作分别存储在变量minA和minB中。
- minA和minB的最小值就是这个问题的答案。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to return the minimum operations
// required to make all string characters equal
int minOperations(string str)
{
int n = str.size();
// Vector to store the
// frequency of all characters
vector freq(26, 0);
for (auto x : str) {
freq[x - 'a']++;
}
int mxA = INT_MIN, mxB = INT_MIN;
// Variables to store
// consonant and vowel
// with highest frequency
char A, B;
vector vowels
= { 'a', 'e', 'i', 'o', 'u' };
for (int i = 0; i < 26; ++i) {
bool isVowel = 0;
for (auto x : vowels) {
if ('a' + i == x) {
isVowel = 1;
break;
}
}
// If current character is a vowel
if (isVowel) {
if (mxB < freq[i]) {
mxB = freq[i];
B = 'a' + i;
}
}
// If current character is a consonant
else {
if (mxA < freq[i]) {
mxA = freq[i];
A = 'a' + i;
}
}
}
int minA = 0, minB = 0;
for (auto x : str) {
bool isVowel = 0;
for (auto y : vowels) {
if (x == y) {
isVowel = 1;
break;
}
}
// If current character is a vowel
if (isVowel) {
if (x != B) {
minB += 2;
}
minA += 1;
}
// If currenct character is a
// consonant
else {
if (x != A) {
minA += 2;
}
minB += 1;
}
}
// If no vowel exists
if (mxB == INT_MIN) {
return minA;
}
// If no consonant exists
if (mxA == INT_MIN) {
return minB;
}
// Returning minimum of the two
return min(minA, minB);
}
// Driver Code
int main()
{
string str = "hexon";
cout << minOperations(str);
}
Java
// Java program for the above approach
import java.util.*;
public class GFG {
// Function to return the minimum operations
// required to make all string characters equal
static int minOperations(String str)
{
int n = str.length();
// Vector to store the
// frequency of all characters
char[] freq = new char[26];
for(char x : str.toCharArray()) { freq[x - 'a']++; }
int mxA = Integer.MIN_VALUE, mxB = Integer.MIN_VALUE;
// Variables to store
// consonant and vowel
// with highest frequency
char A = ' ', B = ' ';
char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
for (int i = 0; i < 26; ++i) {
int isVowel = 0;
for(char x : vowels)
{
if ('a' + i == x) {
isVowel = 1;
break;
}
}
// If current character is a vowel
if (isVowel > 0) {
if (mxB < freq[i]) {
mxB = freq[i];
B = (char)(97 + i);
}
}
// If current character is a consonant
else {
if (mxA < freq[i]) {
mxA = freq[i];
A = (char)(97 + i);
}
}
}
int minA = 0, minB = 0;
for(char x : str.toCharArray())
{
int isVowel = 0;
for(char y : vowels)
{
if (x == y) {
isVowel = 1;
break;
}
}
// If current character is a vowel
if (isVowel > 0) {
if (x != B) {
minB += 2;
}
minA += 1;
}
// If currenct character is a
// consonant
else {
if (x != A) {
minA += 2;
}
minB += 1;
}
}
// If no vowel exists
if (mxB == Integer.MIN_VALUE) {
return minA;
}
// If no consonant exists
if (mxA == Integer.MIN_VALUE) {
return minB;
}
// Returning minimum of the two
return Math.min(minA, minB);
}
// Driver Code
public static void main(String args[])
{
String str = "hexon";
System.out.println(minOperations(str));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# python program for the above approach
INT_MIN = -2147483647 - 1
# Function to return the minimum operations
# required to make all string characters equal
def minOperations(str):
n = len(str)
# Vector to store the
# frequency of all characters
freq = [0 for _ in range(26)]
for x in str:
freq[ord(x) - ord('a')] += 1
mxA = INT_MIN
mxB = INT_MIN
# Variables to store
# consonant and vowel
# with highest frequency
A = ''
B = ''
vowels = ['a', 'e', 'i', 'o', 'u']
for i in range(0, 26):
isVowel = 0
for x in vowels:
if (ord('a') + i == ord(x)):
isVowel = 1
break
# If current character is a vowel
if (isVowel):
if (mxB < freq[i]):
mxB = freq[i]
B = chr(ord('a') + i)
# If current character is a consonant
else:
if (mxA < freq[i]):
mxA = freq[i]
A = chr(ord('a') + i)
minA = 0
minB = 0
for x in str:
isVowel = 0
for y in vowels:
if (x == y):
isVowel = 1
break
# If current character is a vowel
if (isVowel):
if (x != B):
minB += 2
minA += 1
# If currenct character is a
# consonant
else:
if (x != A):
minA += 2
minB += 1
# If no vowel exists
if (mxB == INT_MIN):
return minA
# If no consonant exists
if (mxA == INT_MIN):
return minB
# Returning minimum of the two
return min(minA, minB)
# Driver Code
if __name__ == "__main__":
str = "hexon"
print(minOperations(str))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG {
// Function to return the minimum operations
// required to make all string characters equal
static int minOperations(string str)
{
int n = str.Length;
// Vector to store the
// frequency of all characters
char[] freq = new char[26];
foreach(char x in str) { freq[x - 'a']++; }
int mxA = Int32.MinValue, mxB = Int32.MinValue;
// Variables to store
// consonant and vowel
// with highest frequency
char A = ' ', B = ' ';
char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
for (int i = 0; i < 26; ++i) {
int isVowel = 0;
foreach(char x in vowels)
{
if ('a' + i == x) {
isVowel = 1;
break;
}
}
// If current character is a vowel
if (isVowel > 0) {
if (mxB < freq[i]) {
mxB = freq[i];
B = (char)(97 + i);
}
}
// If current character is a consonant
else {
if (mxA < freq[i]) {
mxA = freq[i];
A = (char)(97 + i);
}
}
}
int minA = 0, minB = 0;
foreach(char x in str)
{
int isVowel = 0;
foreach(char y in vowels)
{
if (x == y) {
isVowel = 1;
break;
}
}
// If current character is a vowel
if (isVowel > 0) {
if (x != B) {
minB += 2;
}
minA += 1;
}
// If currenct character is a
// consonant
else {
if (x != A) {
minA += 2;
}
minB += 1;
}
}
// If no vowel exists
if (mxB == Int32.MinValue) {
return minA;
}
// If no consonant exists
if (mxA == Int32.MinValue) {
return minB;
}
// Returning minimum of the two
return Math.Min(minA, minB);
}
// Driver Code
public static void Main()
{
string str = "hexon";
Console.WriteLine(minOperations(str));
}
}
// This code is contributed by ukasp.
Javascript
输出
5
时间复杂度: O(N)
辅助空间: O(1)