text: Minimize cost to replace all the vowels of a given String by a single vowel | C ++ program for the above approach ; Function that return true if the given character is a vowel ; Function to return the minimum cost to convert all the vowels of a string to a single one ; Stores count of respective vowels ; Iterate through the string ; If a vowel is encountered ; Calculate the cost ; Return the minimum cost ; Driver Code
code: #include <bits/stdc++.h> NEW_LINE 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 ; }