The Wayback Machine - https://web.archive.org/web/20210414112839/https://www.geeksforgeeks.org/minimize-cost-to-convert-all-occurrences-of-each-distinct-character-to-lowercase-or-uppercase/
Skip to content
Related Articles

Related Articles

Minimize cost to convert all occurrences of each distinct character to lowercase or uppercase
  • Last Updated : 14 Apr, 2021
GeeksforGeeks - Summer Carnival Banner

Given a string S consisting of N alphabets and two positive integers L and U, representing the cost of converting any character to lowercase and uppercase respectively, the task is to modify the string S such that every occurrence of the alphabet in the string S is either lowercase or uppercase and the cost of converting the original string to the modified string must be minimum.

Examples:

Input: S = “aabbAA”, L = 1, U = 1
Output: AAbbAA
Explanation:
The cost of converting “aabbAA” to “AAbbAA” is 1*2 = 2, which is minimum among all the possible combinations of conversions.

Input: S = “aApbBp”, L = 1, U = 2
Output: aapbbp

Approach: The given problem can be solved by using the Greedy Approach, the idea is to check if the cost to convert all occurrences of a character to its lowercase is less than that of uppercase, then convert all its occurrences to lowercase. Otherwise, convert all its occurrences to uppercase. Follow the steps below to solve the problem:



  • Store the frequency of all lowercase and uppercase characters in the arrays lowerFreq[26] and upperFreq[26] respectively.
  • Initialize another array say, result[26] as 0 where result[i] = 1 denotes that the ith character needs to be in lowercase, otherwise it needs to be in uppercase.
  • Iterate over the range [0, 25] using the variable i and perform the following steps:
    • Store the cost to convert every occurrence of character i to lowercase and uppercase in variables costLower and costUpper respectively.
    • Find the value of costLower by multiplying the cost to convert 1 character to lowercase, i.e., L with its uppercase frequencies, i.e., upperFreq[i]. Similarly, find the value of costUpper by multiplying U with lowerFreq[i].
    • If the value of costLower < costUpper, then update the value of result[i] to 1.
  • Traverse the string S using the variable i and perform the following steps:
  • After completing the above steps, print the string S as the modified string.

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
// to convert all distinct characters
// to either uppercase or lowercase
void minimumCost(string s, int L, int U)
{
    // Store the size of the string
    int N = s.size();
  
    string ans = "";
  
    // Stores the frequency of lowercase
    // & uppercase characters respectively
    int lowerFreq[26] = { 0 };
    int upperFreq[26] = { 0 };
  
    // Traverse the string S
    for (int i = 0; i < N; i++) {
  
        // Update uppercase
        // frequency of s[i]
        if (isupper(s[i]))
            upperFreq[s[i] - 'A']++;
  
        // Otherwise, update lowercase
        // frequency of s[i]
        else
            lowerFreq[s[i] - 'a']++;
    }
  
    // Stores if the i-th character
    //should be lowercase or not
    int result[26] = { 0 };
  
    // Iterate over the range [0, 25]
    for (int i = 0; i < 26; i++) {
  
        // If the character is present
        // in the string
        if (lowerFreq[i] != 0
            || upperFreq[i] != 0) {
  
            // Store the cost to convert
            // every occurence of i to
            // uppercase and lowercase
            int costToUpper = U * lowerFreq[i];
            int costToLower = L * upperFreq[i];
  
            // Update result[i] to 1 if
            // lowercase cost is less
            if (costToLower < costToUpper) {
                result[i] = 1;
            }
        }
    }
  
    // Traverse the string S
    for (int i = 0; i < N; i++) {
  
        // Store the index
        // of the character
        int index = 0;
  
        if (islower(s[i]))
            index = s[i] - 'a';
        else
            index = s[i] - 'A';
  
        // Convert the current character
        // to uppercase or lowercase
        // according to the condition
        if (result[index] == 1) {
  
            // Update s[i]
            s[i] = tolower(s[i]);
        }
        else {
  
            // Update s[i]
            s[i] = toupper(s[i]);
        }
    }
  
    // Print the modified string
    cout << s;
}
  
// Driver Code
int main()
{
    string S = "aabbAA";
    int L = 1, U = 1;
    minimumCost(S, L, U);
  
    return 0;
}
Output:
AAbbAA

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

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
Recommended Articles
Page :