Open In App

Minimum sum of squares of character counts in a given string after removing k characters

Last Updated : 27 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a string s of lowercase alphabets and a number k, find the minimum value of the string after removal of k characters. The value of a string is defined as the sum of squares of the count of each distinct character. 

Examples: 

Input: s = "abbccc", k = 2
Output: 6
Explanation: We remove two 'c' to get the value as 12 + 22 + 12 or We remove one 'b' and one 'c' to get the value 12 + 12 + 22.

Input: s = "aaab", k = 2
Output: 2
Explanation: We remove two 'a'. Now we get the value as 12 + 12.

[Naive Approach] Frequency Count Reduction via Sorting

The main idea is to minimize the sum of squares of character frequencies by always reducing the highest frequency character first. Since higher frequencies contribute more to the square sum, decreasing them has the greatest impact. We count the frequency of each character, and for k steps, we reduce the current maximum frequency by 1, sorting after each reduction to maintain order. This balances the frequency distribution and lowers the overall square sum effectively.

C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int minValue(string &s, int k) {
    
    vector<int> freq(26, 0);
    
    // Count frequency of each character
    for (char ch : s) {
        freq[ch - 'a']++;
    }

    // Perform k removals
    while (k--) {
        
        // Sort to bring the highest frequency at the end
        sort(freq.begin(), freq.end());

        // If the highest frequency is already 0, break early
        if (freq[25] == 0) break;

        // Decrease the highest frequency by 1
        freq[25]--;
    }

    // Calculate the sum of squares
    int result = 0;
    for (int count : freq) {
        result += count * count;
    }

    return result;
}

int main() {
    string s = "abbccc";
    int k = 2;
    cout << minValue(s, k) << endl;  
    return 0;
}
Java
import java.util.Arrays;

class GfG{

    static int minValue(String s, int k) {
        
        int[] freq = new int[26];

        // Count frequency of each character
        for (char c : s.toCharArray()) {
            freq[c - 'a']++;
        }

        // Reduce highest frequency character k times
        while (k > 0) {
            
            // Sort so highest frequency is at the end
            Arrays.sort(freq); 
            
            // No more characters to reduce
            if (freq[25] == 0) break; 
            
            // Reduce the highest frequency by 1
            freq[25]--; 
            k--;
        }

        // Calculate sum of squares of remaining frequencies
        int result = 0;

        for (int f : freq) {
            result += f * f;
        }

        return result;
    }

    public static void main(String[] args) {
        
        String s = "abbccc";
        int k = 2;
        System.out.println(minValue(s, k));  
    }
}
Python
def minValue(s, k):
    freq = [0] * 26

    # Count frequency of each character
    for c in s:
        freq[ord(c) - ord('a')] += 1

    # Reduce the highest frequency character k times
    while k > 0:
        
        # Sort so the max frequency is at the end
        freq.sort() 
        
        # No characters left to reduce
        if freq[25] == 0:
            break  
        
        # Reduce the max frequency
        freq[25] -= 1  
        k -= 1

    # Calculate sum of squares of frequencies
    result = sum(f * f for f in freq)
    return result

if __name__ == "__main__":
    s = "abbccc"
    k = 2
    print(minValue(s, k))
C#
using System;

class GfG{

    public static int minValue(string s, int k){
        
        int[] freq = new int[26];

        // Count frequency of each character
        foreach (char c in s){
            
            freq[c - 'a']++;
        }

        // Reduce highest frequency character k times
        while (k > 0){
            
            // Sort so max frequency is at the end
            Array.Sort(freq); 
            
            // No characters left to reduce
            if (freq[25] == 0) break;
            
            // Reduce the max frequency
            freq[25]--; 
            k--;
        }

        // Calculate sum of squares of frequencies
        int result = 0;
        foreach (int f in freq){
            
            result += f * f;
        }

        return result;
    }

    // Main method for testing
    public static void Main(){
        
        string s = "abbccc";
        int k = 2;
        Console.WriteLine(minValue(s, k)); 
    }
}
JavaScript
function minValue(s, k) {
    
    const freq = Array(26).fill(0);

    // Count frequency of each character
    for (let i = 0; i < s.length; i++) {
        freq[s.charCodeAt(i) - 'a'.charCodeAt(0)]++;
    }

    // Reduce the highest frequency character k times
    while (k > 0) {
        
        // Sort ascending
        freq.sort((a, b) => a - b); 
        
        // No character to reduce
        if (freq[25] === 0) break;  
        
        // Decrease highest frequency
        freq[25]--;                 
        k--;
    }

    // Calculate sum of squares of frequencies
    let result = 0;
    for (let f of freq) {
        result += f * f;
    }

    return result;
}

// Driver Code
let s = "abbccc";
let k = 2;
console.log(minValue(s, k));

Output
6

Time Complexity: O(n + k * 26 log 26), for finding the frequency it's need O(n) and for each of the k steps, sorting the frequency array of size 26 takes constant time.

Auxiliary Space: O(1), Uses a fixed-size array freq array of size 26, so space usage is constant.

[Better Approach] Using Priority Queue - O(n + k log (26)) Time and O(1) Space

The main idea behind the solution is to reduce the impact of the most frequent characters in the string to minimize the sum of the squares of their frequencies. Since higher frequencies contribute more to the total sum (because of squaring), the algorithm uses a greedy approach: it removes one occurrence at a time from the most frequent character k times. After each removal, it recalculates the total by summing the squares of the remaining frequencies. This efficiently ensures the minimized total value.

C++
#include <iostream>
#include <string>
#include <vector>
#include <queue>
using namespace std;

int minValue(string &s, int k) {
    int n = s.length();

    // If k is greater than or equal to the string length, return 0
    if (k >= n) return 0;

    // Frequency array for characters 'a' to 'z'
    const int ALPHABET_SIZE = 26;
    vector<int> freq(ALPHABET_SIZE, 0);
    for (char ch : s) {
        freq[ch - 'a']++;
    }

    // Max heap to always remove the character with highest frequency
    priority_queue<int> pq;
    for (int f : freq) {
        if (f > 0)
            pq.push(f);
    }

    // Remove k characters from the most frequent characters
    while (k-- && !pq.empty()) {
        int top = pq.top();
        pq.pop();
        if (top > 1)
            pq.push(top - 1);
    }

    // Calculate the sum of squares of remaining frequencies
    int result = 0;
    while (!pq.empty()) {
        int f = pq.top();
        pq.pop();
        result += f * f;
    }

    return result;
}

int main() {
    string s = "abbccc";
    int k = 2;

    cout << minValue(s, k) << endl; 

    return 0;
}
Java
import java.util.PriorityQueue;
import java.util.Collections;

class GfG {

    static int minValue(String s, int k) {
        int n = s.length();

        if (k >= n) return 0;

        // Frequency array for characters 'a' to 'z'
        int[] freq = new int[26];
        for (char ch : s.toCharArray()) {
            freq[ch - 'a']++;
        }

        // Max heap (priority queue in descending order)
        PriorityQueue<Integer> pq = 
            new PriorityQueue<>(Collections.reverseOrder());
        
        for (int f : freq) {
            if (f > 0) pq.add(f);
        }

        // Remove k characters from the most frequent characters
        while (k-- > 0 && !pq.isEmpty()) {
            int top = pq.poll();
            if (top > 1) {
                pq.add(top - 1);
            }
        }

        // Calculate the sum of squares
        int result = 0;
        while (!pq.isEmpty()) {
            int f = pq.poll();
            result += f * f;
        }

        return result;
    }

    public static void main(String[] args) {
        String s = "abbccc";
        int k = 2;
        System.out.println(minValue(s, k));
    }
}
Python
import heapq

def minValue(s, k):
    n = len(s)
    if k >= n:
        return 0

    # Frequency of each character
    freq = [0] * 26
    for ch in s:
        freq[ord(ch) - ord('a')] += 1

    # Use a max heap (by negating values)
    max_heap = [-f for f in freq if f > 0]
    heapq.heapify(max_heap)

    # Remove k characters
    while k > 0 and max_heap:
        top = heapq.heappop(max_heap)
        
        # decrement the frequency
        top += 1 
        if top != 0:
            heapq.heappush(max_heap, top)
        k -= 1

    # Compute sum of squares
    return sum(x * x for x in max_heap)
    
# Driver code
if __name__ == "__main__":
    s = "abbccc"
    k = 2
    print(minValue(s, k)) 
C#
using System;
using System.Collections.Generic;

class GfG{
  
    static int minValue(string s, int k){
        
        int n = s.Length;

        // If k is greater than or equal to string length, return 0
        if (k >= n)
            return 0;

        // Count frequency of each character (a-z)
        int[] freq = new int[26];
        foreach (char c in s)
            freq[c - 'a']++;

        // Add non-zero frequencies to a list
        List<int> counts = new List<int>();
        foreach (int f in freq)
            if (f > 0)
                counts.Add(f);

        // Remove k characters by decreasing the max frequency each time
        while (k > 0 && counts.Count > 0){
            
            // Sort in descending order to get the max
            // frequency at the front
            counts.Sort();
            counts.Reverse();

            // Decrease the highest frequency
            if (counts[0] > 0)
                counts[0]--;

            k--;
        }

        // Compute sum of squares of remaining frequencies
        int result = 0;
        foreach (int count in counts)
            result += count * count;

        return result;
    }

    // Driver code
    static void Main(){
        string s = "abbccc";
        int k = 2;
        Console.WriteLine(minValue(s, k));
    }
}
JavaScript
function minValue(s, k) {
    const n = s.length;

    // If k >= length, all characters can be removed
    if (k >= n) return 0;

    // Count frequency of each lowercase letter
    const freq = Array(26).fill(0);
    for (let i = 0; i < n; i++) {
        
        const index = s.charCodeAt(i) - 'a'.charCodeAt(0);
        freq[index]++;
    }

    // Create an array of non-zero frequencies
    const q = [];
    for (let i = 0; i < 26; i++) {
        if (freq[i] > 0) {
            q.push(freq[i]);
        }
    }

    // Remove k characters from the highest frequencies
    while (k > 0) {
        // Sort descending to bring max to front
        q.sort((a, b) => b - a);

        // Decrease the max frequency
        if (q[0] > 0) {
            q[0]--;
            k--;
        }
    }

    // Calculate sum of squares of remaining frequencies
    let result = 0;
    for (let count of q) {
        result += count * count;
    }

    return result;
}

// Driver Code
let s = "abbccc";
let k = 2;
console.log(minValue(s, k));

Output
6

[Expected Approach] Bucket-Based Frequency Reduction - O(n) Time and O(n) Space

The idea is to prioritizing the most frequent ones. By tracking how many characters have each frequency, we reduce the highest frequencies first since they contribute the most to the total sum. Each removal either decreases all characters at a max frequency or partially reduces some of them. After all removals, we compute the sum of squares of the remaining frequencies. This greedy approach ensures the minimal possible total.

Illustration:

C++
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;


int minValue(string &s, int k) {
    
    int n = s.length();
    int alphabetCount[26] = {0};
    
    int maxi = 0;
    
    // Count frequency of each character
    for (char c : s) {
        alphabetCount[c - 'a']++;
        maxi = max(alphabetCount[c-'a'], maxi);
    }
    
    // frequency bucket 
    vector<int> freq(maxi+1, 0);
    
    int maxFreq = 0;
    
    // Fill frequency bucket
    for (int i = 0; i < 26; i++) {
        if (alphabetCount[i] > 0) {
            freq[alphabetCount[i]]++;
            maxFreq = max(maxFreq, alphabetCount[i]);
        }
    }

    // Reduce frequencies using k removals
    while (k > 0 && maxFreq > 0) {
        int countAtMax = freq[maxFreq];
        if (countAtMax <= k) {
            k -= countAtMax;
            freq[maxFreq - 1] += countAtMax;
            freq[maxFreq] = 0;
            maxFreq--;
        } else {
            freq[maxFreq] -= k;
            freq[maxFreq - 1] += k;
            k = 0;
        }
    }

    int result = 0;
    for (int i = 1; i <= maxi; i++) {
        result += i * i * freq[i];
    }

    return result;
}

// Driver code
int main() {
    
    string s = "abbccc";
    int k = 2;
    cout << minValue(s, k) << endl;
    return 0;
}
Java
import java.util.*;

class GfG{

    public static int minValue(String s, int k) {
        int n = s.length();
        int[] alphabetCount = new int[26];
        int maxi = 0;
        
        // Count frequency of each character
        for (char c : s.toCharArray()) {
            alphabetCount[c - 'a']++;
            maxi = Math.max(maxi, alphabetCount[c-'a']);
        }

        int[] freq = new int[maxi + 1];
        int maxFreq = 0;

        // Fill frequency bucket
        for (int count : alphabetCount) {
            if (count > 0) {
                freq[count]++;
                maxFreq = Math.max(maxFreq, count);
            }
        }

        // Reduce frequencies using k removals
        while (k > 0 && maxFreq > 0) {
            int countAtMax = freq[maxFreq];
            if (countAtMax <= k) {
                k -= countAtMax;
                freq[maxFreq - 1] += countAtMax;
                freq[maxFreq] = 0;
                maxFreq--;
            } else {
                freq[maxFreq] -= k;
                freq[maxFreq - 1] += k;
                k = 0;
            }
        }

        int result = 0;
        for (int i = 1; i <= maxi; i++) {
            result += i * i * freq[i];
        }

        return result;
    }

    public static void main(String[] args) {
        String s = "abbccc";
        int k = 2;
        System.out.println(minValue(s, k));
    }
}
Python
def minValue(s, k):
    n = len(s)
    # Count of each letter (a-z)
    alphabetCount = [0] * 26 
    
    maxi = 0
    # Count frequency of each character
    for c in s:
        alphabetCount[ord(c) - ord('a')] += 1
        maxi = max(maxi, alphabetCount[ord(c) - ord('a')])
        
    maxFreq = 0

    # freq[i] = number of characters with frequency i
    freq = [0] * (maxi + 1)      

    # Fill frequency bucket and track the maximum frequency
    for count in alphabetCount:
        if count > 0:
            freq[count] += 1
            maxFreq = max(maxFreq, count)

    # Remove k characters by reducing higher frequencies
    while k > 0 and maxFreq > 0:
        count_at_max = freq[maxFreq]
        if count_at_max <= k:
            
            # Can remove all characters at this frequency
            k -= count_at_max
            freq[maxFreq - 1] += count_at_max
            freq[maxFreq] = 0
            maxFreq -= 1
        else:
            
            # Partially remove only k characters
            freq[maxFreq] -= k
            freq[maxFreq - 1] += k
            k = 0

    # Calculate the result: sum of (freq^2 * number of chars with that freq)
    result = 0
    for i in range(1, maxi+1):
        result += i * i * freq[i]

    return result

# Driver code
if __name__ == "__main__":
    s = "abbccc"
    k = 2
    print(minValue(s, k))
C#
using System;

class GfG{

    static int MinValue(string s, int k) {
        int n = s.Length;
        int[] alphabetCount = new int[26];
        int maxi = 0;

        // Count frequency of each character
        foreach (char c in s) {
            alphabetCount[c - 'a']++;
            maxi = Math.Max(maxi, alphabetCount[c - 'a']);
        }
        
        int[] freq = new int[maxi + 1];
        int maxFreq = 0;

        // Fill frequency bucket
        foreach (int count in alphabetCount) {
            if (count > 0) {
                freq[count]++;
                maxFreq = Math.Max(maxFreq, count);
            }
        }

        // Reduce frequencies using k removals
        while (k > 0 && maxFreq > 0) {
            int countAtMax = freq[maxFreq];
            if (countAtMax <= k) {
                k -= countAtMax;
                freq[maxFreq - 1] += countAtMax;
                freq[maxFreq] = 0;
                maxFreq--;
            } else {
                freq[maxFreq] -= k;
                freq[maxFreq - 1] += k;
                k = 0;
            }
        }

        int result = 0;
        for (int i = 1; i <= maxi; i++) {
            result += i * i * freq[i];
        }

        return result;
    }

    static void Main() {
        string s = "abbccc";
        int k = 2;
        Console.WriteLine(MinValue(s, k));
    }
}
JavaScript
function minValue(s, k) {
    const n = s.length;
    
    // Frequency of 'a' to 'z'
    const alphabetCount = Array(26).fill(0);  
    
    let maxi = 0;
    
    // Count frequency of each character
    for (let i = 0; i < n; i++) {
        alphabetCount[s.charCodeAt(i) - 97]++;
        maxi = Math.max(maxi, alphabetCount[s.charCodeAt(i) - 97]);
    }

    let maxFreq = 0;
    
    // freq[i] = number of characters with frequency i
    const freq = Array(maxi + 1).fill(0);        
    
    // Fill frequency bucket and find the max frequency
    for (let i = 0; i < 26; i++) {
        const count = alphabetCount[i];
        if (count > 0) {
            freq[count]++;
            maxFreq = Math.max(maxFreq, count);
        }
    }

    // Reduce highest frequencies first until k becomes 0
    while (k > 0 && maxFreq > 0) {
        const countAtMax = freq[maxFreq];
        if (countAtMax <= k) {
        
            // Remove one occurrence from each character at maxFreq
            k -= countAtMax;
            freq[maxFreq - 1] += countAtMax;
            freq[maxFreq] = 0;
            maxFreq--;
        } 
        else {
            
            // Only part of the characters can be reduced
            freq[maxFreq] -= k;
            freq[maxFreq - 1] += k;
            k = 0;
        }
    }

    // Compute the final sum of squares
    let result = 0;
    for (let i = 1; i <= maxi; i++) {
        result += i * i * freq[i];
    }

    return result;
}

// Driver code
const s = "abbccc";
const k = 2;
console.log(minValue(s, k));

Output
6

Game with String | DSA Problem
Article Tags :
Practice Tags :

Similar Reads