# Minimum cost to convert given string to consist of only vowels

Given string str of lower case alphabets, the task is to find the minimum cost to change the input string in a string that contains only vowels. Each consonant is changed to the nearest vowels. The cost is defined as the absolute difference between the ASCII value of consonant and vowel.

Examples:

Input: str = “abcde”
Output: 4
Explanation:
Here, a and e are already the vowels but b, c, and d are consonants. So b, c, and d are changed to nearest vowels as:
1. b –> a  = |98 – 97| = 1,
2. c –> a  = |99 – 97| = 2  or c –> e  = |99 – 101| = 2 ( to minimum the cost),
3. d –> e  = |100 – 101| = 1.
Therefore, the minimum cost is 1 + 2 + 1 = 4.

Input: str = “aaa”
Output: 0
Explanation:
There is no consonant in the string.

Approach: The idea is to traverse over the string and replace each consonant with their nearest vowel and add the cost as the difference between the consonants and changed vowel. Print the cost after all operations.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach  ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find the minimum cost  ` `int` `min_cost(string st) ` `{ ` `     `  `    ``// Store vowels  ` `    ``string vow = ``"aeiou"``; ` `    ``int` `cost = 0; ` ` `  `    ``// Loop for iteration of string  ` `    ``for``(``int` `i = 0; i < st.size(); i++) ` `    ``{ ` `        ``vector<``int``> costs;  ` ` `  `        ``// Loop to calculate the cost  ` `        ``for``(``int` `j = 0; j < 5; j++)  ` `            ``costs.push_back(``abs``(st[i] - vow[j]));  ` `             `  `        ``// Add minimum cost  ` `        ``cost += *min_element(costs.begin(),  ` `                             ``costs.end());  ` `    ``} ` `    ``return` `cost; ` `} ` ` `  `// Driver Code  ` `int` `main() ` `{ ` `     `  `    ``// Given String  ` `    ``string str = ``"abcde"``; ` `     `  `    ``// Function Call  ` `    ``cout << (min_cost(str)); ` `} ` ` `  `// This code is contributed by grand_master `

## Python3

 `# Python3 program for above approach ` ` `  `# Function to find the minimum cost ` `def` `min_cost(st): ` ` `  `    ``# Store vowels ` `    ``vow ``=` `"aeiou"` `    ``cost ``=` `0` ` `  `    ``# Loop for iteration of string ` `    ``for` `i ``in` `range``(``len``(st)): ` `        ``costs ``=` `[] ` ` `  `        ``# Loop to calculate the cost ` `        ``for` `j ``in` `range``(``5``): ` `            ``costs.append(``abs``(``ord``(st[i])``-``ord``(vow[j]))) ` `             `  `        ``# Add minimum cost ` `        ``cost ``+``=` `min``(costs) ` `    ``return` `cost ` ` `  `# Driver Code ` ` `  `# Given String ` `str` `=` `"abcde"` ` `  `# Function Call ` `print``(min_cost(``str``)) `

Output:

```4
```

Time Complexity: O(N), where N is the length of the string.
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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : grand_master