Open In App

# Minimize cost to replace all the vowels of a given String by a single vowel

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:
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 ``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`

## Javascript

 ``

Output:

`10`

Time Complexity: O(N)
Auxiliary Space: O(1)