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.
Input: str = “abcde”
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”
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:
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.
- 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 cost required to convert all Subarrays of size K to a single element
- 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 steps to convert one binary string to other only using negation
- Minimum reduce operations to convert a given string into a palindrome
- Minimum number of subsequences required to convert one string to another
- Minimum swaps required to convert one binary string to another
- Minimum given operations required to convert a given binary string to all 1's
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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