Minimum sum of squares of character counts in a given string after removing k characters
Last Updated :
27 Jun, 2025
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));
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));
[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));
Game with String | DSA Problem
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem