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++
// C++ program for the above approach #include <bits/stdc++.h> 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 |
10
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Replace every vowels with lexicographically next vowel in a String
- Replace all consonants with nearest vowels in a string
- Minimum cost to convert given string to consist of only vowels
- Minimize cost to convert all characters of a binary string to 0s
- Print all Subsequences of String which Start with Vowel and End with Consonant.
- Minimize cost to empty a given string by removing characters alphabetically
- Minimize cost to convert given string into concatenation of equal substrings of length K
- Python program to check if given string is vowel Palindrome
- Check if the given string is vowel prime
- Minimize Cost to sort a String in Increasing Order of Frequencies of Characters
- Alternate vowel and consonant string
- Lexicographically first alternate vowel and consonant string
- Count the number of vowels occurring in all the substrings of given string
- Minimum swaps required to move all vowels occurs after consonants in a given string
- Substrings starting with vowel and ending with consonants and vice versa
- Arrangement of words without changing the relative position of vowel and consonants
- Count of adjacent Vowel Consonant Pairs
- Program to accept Strings starting with a Vowel
- Count of N size strings consisting of at least one vowel and one consonant
- Count pair of strings whose concatenation has every vowel
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

