📌  相关文章
📜  删除所有出现的一个字符后最小化 ASCII 值总和

📅  最后修改于: 2022-05-13 01:57:07.831000             🧑  作者: Mango

删除所有出现的一个字符后最小化 ASCII 值总和

给定字符串str ,任务是在删除每个出现的特定字符后最小化str的每个字符的 ASCII 值总和。

例子:

方法:

  1. 取给定字符串中所有 ASCII 值的总和。
  2. 此外,存储字符串中每个字符的出现次数。
  3. 删除对总和贡献最大值的字符的每个出现,即其出现 * ASCII是最大的。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the minimized sum
int getMinimizedSum(string str, int len)
{
    int i, maxVal = INT_MIN, sum = 0;
 
    // To store the occurrences of
    // each character of the string
    int occurrences[26] = { 0 };
    for (i = 0; i < len; i++) {
 
        // Update the occurrence
        occurrences[str[i] - 'a']++;
 
        // Calculate the sum
        sum += (int)str[i];
    }
 
    // Get the character which is contributing
    // the maximum value to the sum
    for (i = 0; i < 26; i++)
 
        // Count of occurrence of the character
        // multiplied by its ASCII value
        maxVal = max(maxVal, occurrences[i] * (i + 'a'));
 
    // Return the minimized sum
    return (sum - maxVal);
}
 
// Driver code
int main()
{
    string str = "geeksforgeeks";
    int len = str.length();
    cout << getMinimizedSum(str, len);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.Arrays;
import java.lang.Math;
 
class GfG
{
 
    // Function to return the minimized sum
    static int getMinimizedSum(String str, int len)
    {
        int i, maxVal = Integer.MIN_VALUE, sum = 0;
     
        // To store the occurrences of
        // each character of the string
        int occurrences[] = new int[26];
        Arrays.fill(occurrences, 0);
         
        for (i = 0; i < len; i++)
        {
     
            // Update the occurrence
            occurrences[str.charAt(i) - 'a']++;
     
            // Calculate the sum
            sum += (int)str.charAt(i);
        }
     
        // Get the character which is contributing
        // the maximum value to the sum
        for (i = 0; i < 26; i++)
     
            // Count of occurrence of the character
            // multiplied by its ASCII value
            maxVal = Math.max(maxVal, occurrences[i] * (i + 'a'));
     
        // Return the minimized sum
        return (sum - maxVal);
    }
 
    // Driver code
    public static void main(String []args){
         
        String str = "geeksforgeeks";
        int len = str.length();
        System.out.println(getMinimizedSum(str, len));
    }
}
 
// This code is contributed by Rituraj Jain


Python3
# Python3 implementation of the approach
import sys
 
# Function to return the minimized sum
def getMinimizedSum(string, length) :
 
    maxVal = -(sys.maxsize - 1)
    sum = 0;
 
    # To store the occurrences of
    # each character of the string
    occurrences = [0] * 26;
     
    for i in range(length) :
 
        # Update the occurrence
        occurrences[ord(string[i]) -
                    ord('a')] += 1;
 
        # Calculate the sum
        sum += ord(string[i]);
 
    # Get the character which is contributing
    # the maximum value to the sum
    for i in range(26) :
 
        # Count of occurrence of the character
        # multiplied by its ASCII value
        count = occurrences[i] * (i + ord('a'))
        maxVal = max(maxVal, count);
 
    # Return the minimized sum
    return (sum - maxVal);
 
# Driver code
if __name__ == "__main__" :
 
    string = "geeksforgeeks";
    length = len(string);
     
    print(getMinimizedSum(string, length));
 
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
 
class GfG
{
 
    // Function to return the minimized sum
    static int getMinimizedSum(string str, int len)
    {
        int i, maxVal = Int32.MinValue, sum = 0;
     
        // To store the occurrences of
        // each character of the string
        int [] occurrences = new int[26];
         
        for (i = 0; i < len; i++)
        {
     
            // Update the occurrence
            occurrences[str[i] - 'a']++;
     
            // Calculate the sum
            sum += (int)str[i];
        }
     
        // Get the character which is contributing
        // the maximum value to the sum
        for (i = 0; i < 26; i++)
     
            // Count of occurrence of the character
            // multiplied by its ASCII value
            maxVal = Math.Max(maxVal, occurrences[i] * (i + 'a'));
     
        // Return the minimized sum
        return (sum - maxVal);
    }
 
    // Driver code
    public static void Main()
    {
        string str = "geeksforgeeks";
        int len = str.Length;
        Console.WriteLine(getMinimizedSum(str, len));
    }
}
 
// This code is contributed by ihritik


Javascript


输出:
977