给定一个字符串S,任务是找到将给定字符串中的所有元音{a, e, i, o, u}转换为其中任何一个的最小成本。转换元音的成本由 ASCII 值的变化给出。
例子:
Input: S = “geeksforgeeks”
Output: 10
Explanation:
Count of e’s = 4
Count of o’s = 1
Conversion from ‘o’ to ‘e’ costs abs(‘o’ – ‘e’) = 10.
Hence, the output is 10.
Input: S = “coding”
Output: 6
Explanation:
Conversion from ‘o’ to ‘i’ costs abs(‘o’ – ‘i’) = 6.
Hence, the output is 10.
方法:
这个想法是计算将字符串的每个元音转换为元音{a, e, i, o, u}之一的单独成本,并选择成本最低的元音。请按照以下步骤操作:
- 初始化 5 个变量cA、cE、cI、 cO和cU ,代价为0 ,分别存储元音{a, e, i, o, u}的代价。
- 遍历字符串,并为每个元音找到将其转换为所有其他元音的成本,并将其分别存储在变量cA、cE、cI、 cO和cU 中。
- 将所有元音转换为单个元音的最小成本由min(cA, cE, cI, cO, cU) 给出。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function that return true if the
// given character is a vowel
bool isVowel(char ch)
{
if (ch == 'a' or ch == 'e'
or ch == 'i' or ch == 'o'
or ch == 'u')
return true;
else
return false;
}
// Function to return the minimum
// cost to convert all the vowels
// of a string to a single one
int minCost(string S)
{
// Stores count of
// respective vowels
int cA = 0;
int cE = 0;
int cI = 0;
int cO = 0;
int cU = 0;
// Iterate through the string
for (int i = 0; i < S.size(); i++) {
// If a vowel is encountered
if (isVowel(S[i])) {
// Calculate the cost
cA += abs(S[i] - 'a');
cE += abs(S[i] - 'e');
cI += abs(S[i] - 'i');
cO += abs(S[i] - 'o');
cU += abs(S[i] - 'u');
}
}
// Return the minimum cost
return min(min(min(min(cA, cE),
cI),
cO),
cU);
}
// Driver Code
int main()
{
string S = "geeksforgeeks";
cout << minCost(S) << endl;
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function that return true if the
// given character is a vowel
static boolean isVowel(char ch)
{
if (ch == 'a' || ch == 'e' ||
ch == 'i' || ch == 'o' ||
ch == 'u')
return true;
else
return false;
}
// Function to return the minimum
// cost to convert all the vowels
// of a string to a single one
static int minCost(String S)
{
// Stores count of
// respective vowels
int cA = 0;
int cE = 0;
int cI = 0;
int cO = 0;
int cU = 0;
// Iterate through the string
for(int i = 0; i < S.length(); i++)
{
// If a vowel is encountered
if (isVowel(S.charAt(i)))
{
// Calculate the cost
cA += Math.abs(S.charAt(i) - 'a');
cE += Math.abs(S.charAt(i) - 'e');
cI += Math.abs(S.charAt(i) - 'i');
cO += Math.abs(S.charAt(i) - 'o');
cU += Math.abs(S.charAt(i) - 'u');
}
}
// Return the minimum cost
return Math.min(Math.min(Math.min(Math.min(cA, cE),
cI), cO), cU);
}
// Driver code
public static void main(String[] args)
{
String S = "geeksforgeeks";
System.out.println(minCost(S));
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
# Function that return true if the
# given character is a vowel
def isVowel(ch):
if (ch == 'a' or ch == 'e' or
ch == 'i' or ch == 'o' or
ch == 'u'):
return True;
else:
return False;
# Function to return the minimum
# cost to convert all the vowels
# of a string to a single one
def minCost(S):
# Stores count of
# respective vowels
cA = 0;
cE = 0;
cI = 0;
cO = 0;
cU = 0;
# Iterate through the string
for i in range(len(S)):
# If a vowel is encountered
if (isVowel(S[i])):
# Calculate the cost
cA += abs(ord(S[i]) - ord('a'));
cE += abs(ord(S[i]) - ord('e'));
cI += abs(ord(S[i]) - ord('i'));
cO += abs(ord(S[i]) - ord('o'));
cU += abs(ord(S[i]) - ord('u'));
# Return the minimum cost
return min(min(min(min(cA, cE), cI), cO), cU);
# Driver code
S = "geeksforgeeks";
print(minCost(S))
# This code is contributed by grand_master
C#
// C# program for the above approach
using System;
class GFG{
// Function that return true if the
// given character is a vowel
static bool isVowel(char ch)
{
if (ch == 'a' || ch == 'e' ||
ch == 'i' || ch == 'o' ||
ch == 'u')
return true;
else
return false;
}
// Function to return the minimum
// cost to convert all the vowels
// of a string to a single one
static int minCost(String S)
{
// Stores count of
// respective vowels
int cA = 0;
int cE = 0;
int cI = 0;
int cO = 0;
int cU = 0;
// Iterate through the string
for(int i = 0; i < S.Length; i++)
{
// If a vowel is encountered
if (isVowel(S[i]))
{
// Calculate the cost
cA += Math.Abs(S[i] - 'a');
cE += Math.Abs(S[i] - 'e');
cI += Math.Abs(S[i] - 'i');
cO += Math.Abs(S[i] - 'o');
cU += Math.Abs(S[i] - 'u');
}
}
// Return the minimum cost
return Math.Min(Math.Min(
Math.Min(Math.Min(cA, cE),
cI), cO), cU);
}
// Driver code
public static void Main(String[] args)
{
String S = "geeksforgeeks";
Console.WriteLine(minCost(S));
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
10
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live