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
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 ) ) ; } }