小写字母的定的字符串str,任务是找到改变输入字符串中只包含一个元音字母字符串的最低成本。每个辅音都会更改为最近的元音。代价定义为辅音和元音的ASCII值之间的绝对差。
例子:
Input: str = “abcde”
Output: 4
Explanation:
Here, a and e are already the vowels but b, c, and d are consonants. So b, c, and d are changed to nearest vowels as:
1. b –> a = |98 – 97| = 1,
2. c –> a = |99 – 97| = 2 or c –> e = |99 – 101| = 2 ( to minimum the cost),
3. d –> e = |100 – 101| = 1.
Therefore, the minimum cost is 1 + 2 + 1 = 4.
Input: str = “aaa”
Output: 0
Explanation:
There is no consonant in the string.
方法:想法是遍历字符串,并用最接近的元音替换每个辅音,并增加成本作为辅音和更改后的元音之间的差额。完成所有操作后打印成本。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum cost
int min_cost(string st)
{
// Store vowels
string vow = "aeiou";
int cost = 0;
// Loop for iteration of string
for(int i = 0; i < st.size(); i++)
{
vector costs;
// Loop to calculate the cost
for(int j = 0; j < 5; j++)
costs.push_back(abs(st[i] - vow[j]));
// Add minimum cost
cost += *min_element(costs.begin(),
costs.end());
}
return cost;
}
// Driver Code
int main()
{
// Given String
string str = "abcde";
// Function Call
cout << (min_cost(str));
}
// This code is contributed by grand_master
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to find the minimum cost
static int min_cost(String st)
{
// Store vowels
String vow = "aeiou";
int cost = 0;
// Loop for iteration of string
for(int i = 0; i < st.length(); i++)
{
ArrayList costs = new ArrayList<>();
// Loop to calculate the cost
for(int j = 0; j < 5; j++)
costs.add(Math.abs(st.charAt(i) -
vow.charAt(j)));
// Add minimum cost
int minx = Integer.MAX_VALUE;
for(int x : costs)
{
if(x < minx)
{
minx = x;
}
}
cost += minx;
}
return cost;
}
// Driver Code
public static void main(String[] args)
{
// Given string
String str = "abcde";
// Function call
System.out.println(min_cost(str));
}
}
// This code is contributed by rutvik_56
Python3
# Python3 program for above approach
# Function to find the minimum cost
def min_cost(st):
# Store vowels
vow = "aeiou"
cost = 0
# Loop for iteration of string
for i in range(len(st)):
costs = []
# Loop to calculate the cost
for j in range(5):
costs.append(abs(ord(st[i])-ord(vow[j])))
# Add minimum cost
cost += min(costs)
return cost
# Driver Code
# Given String
str = "abcde"
# Function Call
print(min_cost(str))
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the minimum cost
static int min_cost(string st)
{
// Store vowels
string vow = "aeiou";
int cost = 0;
// Loop for iteration of string
for(int i = 0; i < st.Length; i++)
{
List costs = new List();
// Loop to calculate the cost
for(int j = 0; j < 5; j++)
costs.Add(Math.Abs(st[i] -
vow[j]));
// Add minimum cost
int minx = Int32.MaxValue;
foreach(int x in costs)
{
if(x < minx)
{
minx = x;
}
}
cost += minx;
}
return cost;
}
// Driver Code
public static void Main()
{
// Given string
string str = "abcde";
// Function call
Console.WriteLine(min_cost(str));
}
}
// This code is contributed by code_hunt
输出:
4
时间复杂度: O(N),其中N是字符串的长度。
辅助空间: O(1)