Skip to content
Related Articles

Related Articles

Improve Article

Minimum cost to convert given string to consist of only vowels

  • Last Updated : 13 Oct, 2020

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

Java




// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to find the minimum cost
static int min_cost(String st)
{
     
    // Store vowels
    String vow = "aeiou";
    int cost = 0;
 
    // Loop for iteration of string
    for(int i = 0; i < st.length(); i++)
    {
        ArrayList<Integer> costs = new ArrayList<>();
 
        // Loop to calculate the cost
        for(int j = 0; j < 5; j++)
            costs.add(Math.abs(st.charAt(i) -
                              vow.charAt(j)));
             
        // Add minimum cost
        int minx = Integer.MAX_VALUE;
        for(int x : costs)
        {
            if(x < minx)
            {
                minx = x;
            }
        }
        cost += minx;
    }
    return cost;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given string
    String str = "abcde";
     
    // Function call
    System.out.println(min_cost(str));
}
}
 
// This code is contributed by rutvik_56

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

C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
  
// Function to find the minimum cost
static int min_cost(string st)
{
     
    // Store vowels
    string vow = "aeiou";
    int cost = 0;
  
    // Loop for iteration of string
    for(int i = 0; i < st.Length; i++)
    {
        List<int> costs = new List<int>();
  
        // Loop to calculate the cost
        for(int j = 0; j < 5; j++)
            costs.Add(Math.Abs(st[i] -
                              vow[j]));
              
        // Add minimum cost
        int minx = Int32.MaxValue;
        foreach(int x in costs)
        {
            if(x < minx)
            {
                minx = x;
            }
        }
        cost += minx;
    }
    return cost;
}
  
// Driver Code
public static void Main()
{
      
    // Given string
    string str = "abcde";
      
    // Function call
    Console.WriteLine(min_cost(str));
}
}
 
// This code is contributed by code_hunt
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. Participate in the Scholorship Test for First-Step-to-DSA Course for Class 9 to 12 students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :