Given a string S of size N, the task is to find the maximum sum of values assigned to all the alphabets of the string S. The value assigned to all the characters are over the range [1, 26], and the values assigned to the same lowercase and uppercase character is the same.
Examples:
Input: S = “pQrqQPR”
Output: 176
Explanation:
The value of the letter ‘P’ is taken as 25, ‘Q’ is taken as 26, ‘R’ is taken as 24. Now, the sum of values assigned to the characters of the string is 25 + 26 + 24 + 26 + 26 + 25 + 24 = 176, which is maximum among all possible combinations of assigning values.Input: S = “#aAaa$”
Output: 104
Approach: The given problem can be solved by storing the frequency of the alphabets in a frequency array and sorting them in descending order. The idea is to multiply the highest frequency with 26, 2nd highest frequency with 25, and so on. Follow the steps below to solve the given problem:
- Initialize an auxiliary array, say frequency[] that stores the frequency of distinct characters in the string S.
- Traverse the string and at every iteration, if the character ch, then increment the frequency using the following cases:
- Upper Case: increment the value at frequency[ch – ‘A’].
- Lower Case: increment the value at frequency[ch – ‘a’].
- Special Character: continue the loop.
- Sort the array frequency[] in increasing order.
- Initialize a variable, say sum as 0 to store the value of the string.
- Traverse the array in reverse order using a variable i from index 25 to 0, and at every iteration perform the following steps:
- If the value at frequency[i] is 0, then break the loop.
- Otherwise, multiply the value of frequency[i] with i and add it to the variable sum.
- After the above steps, print the value of the sum as the result.
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 max possible sum // of values assigned to each characters // of the given string int maxStrength(string s) { // Initialize a frequency array of // size 26 with all elements as 0 vector< int > frequency(26, 0); for ( char ch : s) { // Lowercase character if (ch >= 'a' && ch <= 'z' ) { frequency[ch - 'a' ]++; } // Uppercase character else if (ch >= 'A' && ch <= 'Z' ) { frequency[ch - 'A' ]++; } } // Sort the frequency array sort(frequency.begin(), frequency.end()); // Stores the maximum sum of value int ans = 0; for ( int i = 25; i >= 0; i--) { // If the frequency of the // current character is > 0 if (frequency[i] > 0) { ans = ans + frequency[i] * (i + 1); } // Otherwise else break ; } // Return the maximum sum obtained return ans; } // Driver Code int main() { string S = "pQrqQPR" ; cout << maxStrength(S); return 0; } |
Java
// java program for the above approach import java.util.*; public class GFG { // Function to find max possible sum // of values assigned to each characters // of the given string static int maxStrength(String s) { // Initialize a frequency array of // size 26 with all elements as 0 int []frequency = new int [ 26 ]; int len = s.length(); for ( int i = 0 ; i < len; i++){ char ch = s.charAt(i); // Lowercase character if (ch >= 'a' && ch <= 'z' ) { frequency[ch - 'a' ]++; } // Uppercase character else if (ch >= 'A' && ch <= 'Z' ) { frequency[ch - 'A' ]++; } } // Sort the frequency array Arrays.sort(frequency); // Stores the maximum sum of value int ans = 0 ; for ( int i = 25 ; i >= 0 ; i--) { // If the frequency of the // current character is > 0 if (frequency[i] > 0 ) { ans = ans + frequency[i] * (i + 1 ); } // Otherwise else break ; } // Return the maximum sum obtained return ans; } // Driver Code public static void main (String[] args) { String S = "pQrqQPR" ; System.out.println(maxStrength(S)); } } // This code is contributed by AnkThon |
Python3
# python program for the above approach # Function to find max possible sum # of values assigned to each characters # of the given string def maxStrength(s): # Initialize a frequency array of # size 26 with all elements as 0 frequency = [ 0 for x in range ( 26 )] for ch in s: # Lowercase character if (ch > = 'a' and ch < = 'z' ): frequency[ ord (ch) - 97 ] + = 1 # Uppercase character elif (ch > = 'A' and ch < = 'Z' ): frequency[ ord (ch) - 65 ] + = 1 # Sort the frequency array frequency.sort() # Stores the maximum sum of value ans = 0 for i in range ( 25 , 0 , - 1 ): # If the frequency of the # current character is > 0 if (frequency[i] > 0 ): ans = ans + frequency[i] * (i + 1 ) # Otherwise else : break # Return the maximum sum obtained return ans # Driver Code S = "pQrqQPR" print (maxStrength(S)) # This code is contributed by amrshkumar3. |
C#
// C# program for the above approach using System; public class GFG { // Function to find max possible sum // of values assigned to each characters // of the given string static int maxStrength( string s) { // Initialize a frequency array of // size 26 with all elements as 0 int []frequency = new int [26]; int len = s.Length; for ( int i = 0; i < len; i++){ char ch = s[i]; // Lowercase character if (ch >= 'a' && ch <= 'z' ) { frequency[ch - 'a' ]++; } // Uppercase character else if (ch >= 'A' && ch <= 'Z' ) { frequency[ch - 'A' ]++; } } // Sort the frequency array Array.Sort(frequency); // Stores the maximum sum of value int ans = 0; for ( int i = 25; i >= 0; i--) { // If the frequency of the // current character is > 0 if (frequency[i] > 0) { ans = ans + frequency[i] * (i + 1); } // Otherwise else break ; } // Return the maximum sum obtained return ans; } // Driver Code public static void Main(String[] args) { string S = "pQrqQPR" ; Console.Write(maxStrength(S)); } } // This code is contributed by code_hunt. |
Javascript
<script> // JavaScript Program to implement // the above approach // Function to find max possible sum // of values assigned to each characters // of the given string function maxStrength(s) { // Initialize a frequency array of // size 26 with all elements as 0 let frequency = new Array(26).fill(0); for (let ch of s) { // Lowercase character if (ch.charCodeAt(0) >= 'a' .charCodeAt(0) && ch.charCodeAt(0) <= 'z' .charCodeAt(0)) { frequency[ch.charCodeAt(0) - 'a' .charCodeAt(0)]++; } // Uppercase character else if (ch.charCodeAt(0) >= 'A' .charCodeAt(0) && ch.charCodeAt(0) <= 'Z' .charCodeAt(0)) { frequency[ch.charCodeAt(0) - 'A' .charCodeAt(0)]++; } } // Sort the frequency array frequency.sort( function (a, b) { return a - b }) // Stores the maximum sum of value let ans = 0; for (let i = 25; i >= 0; i--) { // If the frequency of the // current character is > 0 if (frequency[i] > 0) { ans = ans + frequency[i] * (i + 1); } // Otherwise else break ; } // Return the maximum sum obtained return ans; } // Driver Code let S = "pQrqQPR" ; document.write(maxStrength(S)); // This code is contributed by Potta Lokesh </script> |
176
Time Complexity: O(N* log N)
Auxiliary Space: O(26)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!