![]() |
VOOZH | about |
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.
Table of Content
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.
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.
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.
6
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:
6