Minimum cost to convert given string to consist of only vowels
Last Updated :
02 Mar, 2022
Given string str of lower case alphabets, the task is to find the minimum cost to change the input string in a string that contains only vowels. Each consonant is changed to the nearest vowels. The cost is defined as the absolute difference between the ASCII value of consonant and vowel.
Examples:
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.
Approach: The idea is to traverse over the string and replace each consonant with their nearest vowel and add the cost as the difference between the consonants and changed vowel. Print the cost after all operations.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int min_cost(string st)
{
string vow = "aeiou" ;
int cost = 0;
for ( int i = 0; i < st.size(); i++)
{
vector< int > costs;
for ( int j = 0; j < 5; j++)
costs.push_back( abs (st[i] - vow[j]));
cost += *min_element(costs.begin(),
costs.end());
}
return cost;
}
int main()
{
string str = "abcde" ;
cout << (min_cost(str));
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static int min_cost(String st)
{
String vow = "aeiou" ;
int cost = 0 ;
for ( int i = 0 ; i < st.length(); i++)
{
ArrayList<Integer> costs = new ArrayList<>();
for ( int j = 0 ; j < 5 ; j++)
costs.add(Math.abs(st.charAt(i) -
vow.charAt(j)));
int minx = Integer.MAX_VALUE;
for ( int x : costs)
{
if (x < minx)
{
minx = x;
}
}
cost += minx;
}
return cost;
}
public static void main(String[] args)
{
String str = "abcde" ;
System.out.println(min_cost(str));
}
}
|
Python3
def min_cost(st):
vow = "aeiou"
cost = 0
for i in range ( len (st)):
costs = []
for j in range ( 5 ):
costs.append( abs ( ord (st[i]) - ord (vow[j])))
cost + = min (costs)
return cost
str = "abcde"
print (min_cost( str ))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int min_cost( string st)
{
string vow = "aeiou" ;
int cost = 0;
for ( int i = 0; i < st.Length; i++)
{
List< int > costs = new List< int >();
for ( int j = 0; j < 5; j++)
costs.Add(Math.Abs(st[i] -
vow[j]));
int minx = Int32.MaxValue;
foreach ( int x in costs)
{
if (x < minx)
{
minx = x;
}
}
cost += minx;
}
return cost;
}
public static void Main()
{
string str = "abcde" ;
Console.WriteLine(min_cost(str));
}
}
|
Javascript
<script>
function min_cost(st){
let vow = "aeiou"
let cost = 0
for (let i=0;i<st.length;i++){
let costs = []
for (let j = 0; j < 5; j++)
costs.push(Math.abs(st.charCodeAt(i)-vow.charCodeAt(j)))
cost += Math.min(...costs)
}
return cost
}
let str = "abcde"
document.write(min_cost(str), "</br>" )
</script>
|
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...