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:4Explanation:

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:0Explanation:

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<bits/stdc++.h> ` `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 ` |

*chevron_right*

*filter_none*

## 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` `)) ` |

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Minimum cost to convert string into palindrome
- Minimize cost to replace all the vowels of a given String by a single vowel
- Minimum cost to convert str1 to str2 with the given operations
- Minimum swaps required to move all vowels occurs after consonants in a given string
- Minimum cost to construct a string
- Minimum cost to modify a string
- Minimum cost to traverse from one index to another in the String
- Minimum cost to partition the given binary string
- Minimum cost to make a string free of a subsequence
- Minimum Cost of deletions such that string does not contains same consecutive characters
- Minimum number of given operations required to convert a string to another string
- Convert string X to an anagram of string Y with minimum replacements
- Minimum clicks to convert string X to Y
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum reduce operations to convert a given string into a palindrome
- Minimum steps to convert one binary string to other only using negation
- Minimum given operations required to convert a given binary string to all 1's
- Minimum number of subsequences required to convert one string to another
- Minimum swaps required to convert one binary string to another
- Minimum number of adjacent swaps to convert a string into its given anagram

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.