Minimum sum of squares of character counts in a given string after removing “k” characters
Pattern: Top K
Removing one copy of a letter with count c lowers the sum of squares by 2c - 1, which is largest for the biggest count. So repeat k times: take the largest count from a max heap, lower it by one and put it back.
No verified practice link yet. The guide below states the problem in full, so you can still solve it here.
The problem
Remove exactly k characters from s (lowercase) so that the sum of the squares of the letter counts is as small as
possible, and return that sum. "abccc" with k = 1 gives 6; "aaab" with k = 2 gives 2. If k is at least
the length, the answer is 0.
The catalogue has no practice link for this one.
Think first
- How much does removing one copy of a letter with count c save?
- Which letter saves the most?
- Does an early choice ever block a better one later?
Hints
Brute force, and why it fails
Try every way to share the k removals among the letters: exponential.
The key insight
Each removal should hit the current largest count, and a max heap finds it in O(log 26).
Dry run
| counts | 3 1 1 |
|---|
s = "abccc", k = 1. Counts a 1, b 1, c 3.
The plan
heap = max heap of the counts
repeat k times: c = pop; if c - 1 > 0: push c - 1
return sum of c * c over the heap
Complexity
- Time
O(n + k log 26)- Space
O(26)
Counting is O(n) and each removal is a heap step on at most 26 counts.
Edge cases
- k = 0: the sum of the current squares.
- k at least the length: 0.
- All letters different: each removal saves 1.
I've built my logic, show the code
Java
import java.util.*;
class Solution {
public int minValue(String s, int k) {
int[] count = new int[26];
for (char c : s.toCharArray()) count[c - 'a']++;
PriorityQueue<Integer> heap = new PriorityQueue<>(Collections.reverseOrder()); // largest count on top
for (int c : count) if (c > 0) heap.add(c);
while (k-- > 0 && !heap.isEmpty()) {
int top = heap.poll() - 1; // removing one of the most common letter lowers the sum the most
if (top > 0) heap.add(top);
}
int sum = 0;
for (int c : heap) sum += c * c;
return sum;
}
}C++
class Solution {
public:
int minValue(string s, int k) {
int count[26] = {0};
for (char c : s) count[c - 'a']++;
priority_queue<int> heap; // largest count on top
for (int c : count) if (c > 0) heap.push(c);
while (k-- > 0 && !heap.empty()) {
int top = heap.top() - 1; // removing one of the most common letter lowers the sum the most
heap.pop();
if (top > 0) heap.push(top);
}
int sum = 0;
while (!heap.empty()) { sum += heap.top() * heap.top(); heap.pop(); }
return sum;
}
};Python
import heapq
from collections import Counter
class Solution:
def minValue(self, s: str, k: int) -> int:
heap = [-n for n in Counter(s).values()] # largest count on top
heapq.heapify(heap)
for _ in range(k):
if not heap:
break
top = -heapq.heappop(heap) - 1 # removing one of the most common letter lowers the sum the most
if top > 0:
heapq.heappush(heap, -top)
return sum(n * n for n in heap)Common mistakes
- Removing the rarest letters first, which saves the least.
- Pushing a count of 0 back, which is harmless but useless.