Given a string S, the task is to find the minimum cost to convert all the vowels {a, e, i, o, u} in the given string to any one of them. The cost of converting a vowel is given by the change in ASCII value.
Examples:
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.
Approach:
The idea is to calculate the separate cost for converting every vowel in the string to one of the vowels {a, e, i, o, u} and choose the vowel which gives the minimum cost. Follow the steps below:
- Initialize 5 variables cA, cE, cI, cO, and cU with a cost of 0 which stores the cost of vowels {a, e, i, o, u} respectively.
- Iterate through the string, and for each vowel find the cost to convert it into all the other vowels and store it in the variables cA, cE, cI, cO, and cU respectively.
- The minimum cost to convert all the vowels into a single one is given by min(cA, cE, cI, cO, cU).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char ch)
{
if (ch == 'a' or ch == 'e'
or ch == 'i' or ch == 'o'
or ch == 'u' )
return true ;
else
return false ;
}
int minCost(string S)
{
int cA = 0;
int cE = 0;
int cI = 0;
int cO = 0;
int cU = 0;
for ( int i = 0; i < S.size(); i++) {
if (isVowel(S[i])) {
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 min(min(min(min(cA, cE),
cI),
cO),
cU);
}
int main()
{
string S = "geeksforgeeks" ;
cout << minCost(S) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isVowel( char ch)
{
if (ch == 'a' || ch == 'e' ||
ch == 'i' || ch == 'o' ||
ch == 'u' )
return true ;
else
return false ;
}
static int minCost(String S)
{
int cA = 0 ;
int cE = 0 ;
int cI = 0 ;
int cO = 0 ;
int cU = 0 ;
for ( int i = 0 ; i < S.length(); i++)
{
if (isVowel(S.charAt(i)))
{
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 Math.min(Math.min(Math.min(Math.min(cA, cE),
cI), cO), cU);
}
public static void main(String[] args)
{
String S = "geeksforgeeks" ;
System.out.println(minCost(S));
}
}
|
Python3
def isVowel(ch):
if (ch = = 'a' or ch = = 'e' or
ch = = 'i' or ch = = 'o' or
ch = = 'u' ):
return True ;
else :
return False ;
def minCost(S):
cA = 0 ;
cE = 0 ;
cI = 0 ;
cO = 0 ;
cU = 0 ;
for i in range ( len (S)):
if (isVowel(S[i])):
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 min ( min ( min ( min (cA, cE), cI), cO), cU);
S = "geeksforgeeks" ;
print (minCost(S))
|
C#
using System;
class GFG{
static bool isVowel( char ch)
{
if (ch == 'a' || ch == 'e' ||
ch == 'i' || ch == 'o' ||
ch == 'u' )
return true ;
else
return false ;
}
static int minCost(String S)
{
int cA = 0;
int cE = 0;
int cI = 0;
int cO = 0;
int cU = 0;
for ( int i = 0; i < S.Length; i++)
{
if (isVowel(S[i]))
{
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 Math.Min(Math.Min(
Math.Min(Math.Min(cA, cE),
cI), cO), cU);
}
public static void Main(String[] args)
{
String S = "geeksforgeeks" ;
Console.WriteLine(minCost(S));
}
}
|
Javascript
<script>
function isVowel(ch)
{
if (ch == 'a' || ch == 'e'
|| ch == 'i' || ch == 'o'
|| ch == 'u' )
return true ;
else
return false ;
}
function minCost(S)
{
var cA = 0;
var cE = 0;
var cI = 0;
var cO = 0;
var cU = 0;
for ( var i = 0; i < S.length; i++) {
if (isVowel(S[i])) {
cA += Math.abs(S.charCodeAt(i) - 'a' .charCodeAt(0));
cE += Math.abs(S.charCodeAt(i) - 'e' .charCodeAt(0));
cI += Math.abs(S.charCodeAt(i) - 'i' .charCodeAt(0));
cO += Math.abs(S.charCodeAt(i) - 'o' .charCodeAt(0));
cU += Math.abs(S.charCodeAt(i) - 'u' .charCodeAt(0));
}
}
return Math.min(Math.min(Math.min(Math.min(cA, cE),
cI),
cO),
cU);
}
var S = "geeksforgeeks" ;
document.write(minCost(S));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)