Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmMinimize cost to replace all the vowels of a given String by...

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 = “neveropen” 
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 <bits/stdc++.h>
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 = "neveropen";
 
    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 = "neveropen";
 
    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 = "neveropen";
 
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 = "neveropen";
 
    Console.WriteLine(minCost(S));
}
}
 
// This code is contributed by Rajput-Ji


Javascript




<script>
 
// Javascript program f|| the above approach
 
// Function that return true if the
// given character is a vowel
function isVowel(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
function minCost(S)
{
    // St||es count of
    // respective vowels
    var cA = 0;
    var cE = 0;
    var cI = 0;
    var cO = 0;
    var cU = 0;
 
    // Iterate through the string
    for(var i = 0; i < S.length; i++) {
 
        // If a vowel is encountered
        if (isVowel(S[i])) {
 
            // Calculate the cost
            cA += Math.abs(S.charCodeAt(i) - 'a'.charCodeAt(0));
            cE += Math.abs(S.charCodeAt(i) - 'e'.charCodeAt(0));
            cI += Math.abs(S.charCodeAt(i) - 'i'.charCodeAt(0));
            cO += Math.abs(S.charCodeAt(i) - 'o'.charCodeAt(0));
            cU += Math.abs(S.charCodeAt(i) - 'u'.charCodeAt(0));
        }
    }
 
    // Return the Math.minimum cost
    return Math.min(Math.min(Math.min(Math.min(cA, cE),
                    cI),
                cO),
            cU);
}
 
// Driver Code
var S = "neveropen";
document.write(minCost(S));
 
// This code is contributed by importantly.
</script>


Output: 

10

 

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments