Maximize the String value by assigning values in range [1, 26] to each character
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++
#include <bits/stdc++.h>
using namespace std;
int maxStrength(string s)
{
vector< int > frequency(26, 0);
for ( char ch : s) {
if (ch >= 'a' && ch <= 'z' ) {
frequency[ch - 'a' ]++;
}
else if (ch >= 'A' && ch <= 'Z' ) {
frequency[ch - 'A' ]++;
}
}
sort(frequency.begin(),
frequency.end());
int ans = 0;
for ( int i = 25; i >= 0; i--) {
if (frequency[i] > 0) {
ans = ans + frequency[i] * (i + 1);
}
else
break ;
}
return ans;
}
int main()
{
string S = "pQrqQPR" ;
cout << maxStrength(S);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int maxStrength(String s)
{
int []frequency = new int [ 26 ];
int len = s.length();
for ( int i = 0 ; i < len; i++){
char ch = s.charAt(i);
if (ch >= 'a' && ch <= 'z' ) {
frequency[ch - 'a' ]++;
}
else if (ch >= 'A' && ch <= 'Z' ) {
frequency[ch - 'A' ]++;
}
}
Arrays.sort(frequency);
int ans = 0 ;
for ( int i = 25 ; i >= 0 ; i--) {
if (frequency[i] > 0 ) {
ans = ans + frequency[i] * (i + 1 );
}
else
break ;
}
return ans;
}
public static void main (String[] args) {
String S = "pQrqQPR" ;
System.out.println(maxStrength(S));
}
}
|
Python3
def maxStrength(s):
frequency = [ 0 for x in range ( 26 )]
for ch in s:
if (ch > = 'a' and ch < = 'z' ):
frequency[ ord (ch) - 97 ] + = 1
elif (ch > = 'A' and ch < = 'Z' ):
frequency[ ord (ch) - 65 ] + = 1
frequency.sort()
ans = 0
for i in range ( 25 , 0 , - 1 ):
if (frequency[i] > 0 ):
ans = ans + frequency[i] * (i + 1 )
else :
break
return ans
S = "pQrqQPR"
print (maxStrength(S))
|
C#
using System;
public class GFG
{
static int maxStrength( string s)
{
int []frequency = new int [26];
int len = s.Length;
for ( int i = 0; i < len; i++){
char ch = s[i];
if (ch >= 'a' && ch <= 'z' ) {
frequency[ch - 'a' ]++;
}
else if (ch >= 'A' && ch <= 'Z' ) {
frequency[ch - 'A' ]++;
}
}
Array.Sort(frequency);
int ans = 0;
for ( int i = 25; i >= 0; i--) {
if (frequency[i] > 0) {
ans = ans + frequency[i] * (i + 1);
}
else
break ;
}
return ans;
}
public static void Main(String[] args)
{
string S = "pQrqQPR" ;
Console.Write(maxStrength(S));
}
}
|
Javascript
<script>
function maxStrength(s)
{
let frequency = new Array(26).fill(0);
for (let ch of s) {
if (ch.charCodeAt(0) >= 'a' .charCodeAt(0) && ch.charCodeAt(0) <= 'z' .charCodeAt(0)) {
frequency[ch.charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
else if (ch.charCodeAt(0) >= 'A' .charCodeAt(0) && ch.charCodeAt(0) <= 'Z' .charCodeAt(0)) {
frequency[ch.charCodeAt(0) - 'A' .charCodeAt(0)]++;
}
}
frequency.sort( function (a, b) { return a - b })
let ans = 0;
for (let i = 25; i >= 0; i--) {
if (frequency[i] > 0) {
ans = ans + frequency[i] * (i + 1);
}
else
break ;
}
return ans;
}
let S = "pQrqQPR" ;
document.write(maxStrength(S));
</script>
|
Time Complexity: O(N* log N)
Auxiliary Space: O(26)
Last Updated :
25 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...