通过将 [1, 26] 范围内的值分配给每个字符来最大化字符串值
给定一个大小为N的字符串S ,任务是找到分配给字符串S的所有字母的值的最大总和。分配给所有字符的值都在[1, 26]范围内,并且分配给相同的小写和大写字符的值是相同的。
例子:
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
方法:给定的问题可以通过将字母表的频率存储在频率数组中并按降序排序来解决。这个想法是将最高频率与26相乘,第二个最高频率与25相乘,依此类推。请按照以下步骤解决给定的问题:
- 初始化一个辅助数组,比如frequency[] ,它存储字符串S中不同字符的频率。
- 遍历字符串并在每次迭代时,如果字符ch ,则使用以下情况增加频率:
- 大写:在频率[ch – 'A']处增加值。
- 小写:在频率[ch – 'a']处增加值。
- 特殊字符:继续循环。
- 按升序对数组频率[]进行排序。
- 初始化一个变量,比如sum为0来存储字符串的值。
- 使用从索引25到0的变量i以相反的顺序遍历数组,并在每次迭代时执行以下步骤:
- 如果frequency[i]的值为0 ,则中断循环。
- 否则,将frequency[i]的值乘以i并将其添加到变量sum中。
- 在上述步骤之后,打印sum的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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 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
输出:
176
时间复杂度: O(N* log N)
辅助空间: O(26)