Minimize ASCII values sum after removing all occurrences of one character
Given string str, the task is to minimize the sum of ASCII values of each character of str after removing every occurrence of a particular character.
Examples:
Input: str = “geeksforgeeks”
Output: 977
‘g’ occurs twice -> 2 * 103 = 206
‘e’ occurs 4 times -> 4 * 101 = 404
‘k’ occurs twice -> 2 * 107 = 214
‘s’ occurs twice -> 2 * 115 = 230
‘f’ occurs once -> 1 * 102 = 102
‘o’ occurs once -> 1 * 111 = 111
‘r’ occurs once -> 1 * 114 = 114
Total sum = 1381
In order to minimize the sum, remove all the occurrences of ‘e’ from the string
And the new sum becomes 1381 – 404 = 977
Input: str = “abcd”
Output: 294
Brute Force Approach:
The brute force approach to solve this problem would be to iterate over all the characters of the string and remove every occurrence of each character one by one, calculate the sum of ASCII values of the remaining string, and keep track of the minimum sum obtained. Finally, return the minimum sum obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h> using namespace std; // Function to return the minimized sum int getMinimizedSum(string str, int len) { int minSum = INT_MAX; for ( int i = 0; i < len; i++) { string temp = "" ; for ( int j = 0; j < len; j++) { if (str[j] != str[i]) { temp += str[j]; } } int sum = 0; for ( int j = 0; j < temp.length(); j++) { sum += temp[j]; } minSum = min(minSum, sum); } return minSum; } // Driver code int main() { string str = "geeksforgeeks" ; int len = str.length(); cout << getMinimizedSum(str, len); return 0; } |
Java
import java.util.*; public class Main { // Function to return the minimized sum static int getMinimizedSum(String str, int len) { int minSum = Integer.MAX_VALUE; for ( int i = 0 ; i < len; i++) { String temp = "" ; for ( int j = 0 ; j < len; j++) { if (str.charAt(j) != str.charAt(i)) { temp += str.charAt(j); } } int sum = 0 ; for ( int j = 0 ; j < temp.length(); j++) { sum += temp.charAt(j); } minSum = Math.min(minSum, sum); } return minSum; } // Driver code public static void main(String[] args) { String str = "geeksforgeeks" ; int len = str.length(); System.out.println(getMinimizedSum(str, len)); } } |
Python3
# code def get_minimized_sum(s): min_sum = float ( 'inf' ) n = len (s) for i in range (n): temp = "" for j in range (n): if s[j] ! = s[i]: temp + = s[j] curr_sum = sum ( ord (c) for c in temp) min_sum = min (min_sum, curr_sum) return min_sum # Driver code s = "geeksforgeeks" print (get_minimized_sum(s)) |
C#
using System; class Program { // Function to return the minimized sum static int GetMinimizedSum( string str, int len) { int minSum = int .MaxValue; for ( int i = 0; i < len; i++) { string temp = "" ; for ( int j = 0; j < len; j++) { if (str[j] != str[i]) { temp += str[j]; } } int sum = 0; for ( int j = 0; j < temp.Length; j++) { sum += temp[j]; } minSum = Math.Min(minSum, sum); } return minSum; } // Driver code static void Main( string [] args) { string str = "geeksforgeeks" ; int len = str.Length; Console.WriteLine(GetMinimizedSum(str, len)); } } |
Javascript
// Function to return the minimized sum function getMinimizedSum(str, len) { let minSum = Number.MAX_SAFE_INTEGER; for (let i = 0; i < len; i++) { let temp = "" ; for (let j = 0; j < len; j++) { if (str[j] != str[i]) { temp += str[j]; } } let sum = 0; for (let j = 0; j < temp.length; j++) { sum += temp.charCodeAt(j); } minSum = Math.min(minSum, sum); } return minSum; } // Driver code let str = "geeksforgeeks" ; let len = str.length; console.log(getMinimizedSum(str, len)); |
977
Time Complexity: O(N^2)
Space Complexity: O(1)
Approach:
- Take the sum of all ASCII values in the given string.
- Also, store the occurrences of each of the characters of the string.
- Remove every occurrence of the character which is contributing the maximum value to the sum i.e. whose occurrence * ASCII is maximum.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> 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
<script> // JavaScript implementation of the approach // Function to return the minimized sum function getMinimizedSum(str, len) { var i, maxVal = -2147483648, sum = 0; // To store the occurrences of // each character of the string var occurrences = new Array(26).fill(0); for (i = 0; i < len; i++) { // Update the occurrence occurrences[str[i].charCodeAt(0) - "a" .charCodeAt(0)]++; // Calculate the sum sum += str[i].charCodeAt(0); } // 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" .charCodeAt(0))); } // Return the minimized sum return sum - maxVal; } // Driver code var str = "geeksforgeeks" ; var len = str.length; document.write(getMinimizedSum(str, len)); </script> |
977
Time Complexity: O(n), length of the string
Auxiliary Space: O(26)
Please Login to comment...