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

Given a string of lowercase alphabets and a number k, the task is to print 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. For example consider the string “saideep”, here frequencies of characters are s-1, a-1, i-1, e-2, d-1, p-1 and value of the string is 1^2 + 1^2 + 1^2 + 1^2 + 1^2 + 2^2 = 9.

Expected Time Complexity: O(k*logn)**Examples:**

Input : str = abccc, K = 1 Output : 6 We remove c to get the value as 1^{2}+ 1^{2}+ 2^{2}Input : str = aaab, K = 2 Output : 2

**Asked In : Amazon**

One clear observation is that we need to remove character with highest frequency. One trick is the character ma

A **Simple solution** is to use sorting technique through all current highest frequency reduce up to k times. For After every reduce again sort frequency array.

A **Better Solution** used to Priority Queue which has to the highest element on top.

- Initialize empty priority queue.
- Count frequency of each character and Store into temp array.
- Remove K characters which have highest frequency from queue.
- Finally Count Sum of square of each element and return it.

Below is the implementation of the above idea.

## C++

`// C++ program to find min sum of squares` `// of characters after k removals` `#include <bits/stdc++.h>` `using` `namespace` `std;` `const` `int` `MAX_CHAR = 26;` `// Main Function to calculate min sum of` `// squares of characters after k removals` `int` `minStringValue(string str, ` `int` `k)` `{` ` ` `int` `l = str.length(); ` `// find length of string` ` ` `// if K is greater than length of string` ` ` `// so reduced string will become 0` ` ` `if` `(k >= l)` ` ` `return` `0;` ` ` `// Else find Frequency of each character and` ` ` `// store in an array` ` ` `int` `frequency[MAX_CHAR] = { 0 };` ` ` `for` `(` `int` `i = 0; i < l; i++)` ` ` `frequency[str[i] - ` `'a'` `]++;` ` ` `// Push each char frequency into a priority_queue` ` ` `priority_queue<` `int` `> q;` ` ` `for` `(` `int` `i = 0; i < MAX_CHAR; i++)` ` ` `q.push(frequency[i]);` ` ` `// Removal of K characters` ` ` `while` `(k--) {` ` ` `// Get top element in priority_queue,` ` ` `// remove it. Decrement by 1 and again` ` ` `// push into priority_queue` ` ` `int` `temp = q.top();` ` ` `q.pop();` ` ` `temp = temp - 1;` ` ` `q.push(temp);` ` ` `}` ` ` `// After removal of K characters find sum` ` ` `// of squares of string Value` ` ` `int` `result = 0; ` `// Initialize result` ` ` `while` `(!q.empty()) {` ` ` `int` `temp = q.top();` ` ` `result += temp * temp;` ` ` `q.pop();` ` ` `}` ` ` `return` `result;` `}` `// Driver Code` `int` `main()` `{` ` ` `string str = ` `"abbccc"` `; ` `// Input 1` ` ` `int` `k = 2;` ` ` `cout << minStringValue(str, k) << endl;` ` ` `str = ` `"aaab"` `; ` `// Input 2` ` ` `k = 2;` ` ` `cout << minStringValue(str, k);` ` ` `return` `0;` `}` |

## Java

`// Java program to find min sum of squares` `// of characters after k removals` `import` `java.util.Comparator;` `import` `java.util.PriorityQueue;` `import` `java.util.Collections;` `public` `class` `GFG {` ` ` `static` `final` `int` `MAX_CHAR = ` `26` `;` ` ` `// Main Function to calculate min sum of` ` ` `// squares of characters after k removals` ` ` `static` `int` `minStringValue(String str, ` `int` `k)` ` ` `{` ` ` `int` `l = str.length(); ` `// find length of string` ` ` `// if K is greater than length of string` ` ` `// so reduced string will become 0` ` ` `if` `(k >= l)` ` ` `return` `0` `;` ` ` `// Else find Frequency of each character and` ` ` `// store in an array` ` ` `int` `[] frequency = ` `new` `int` `[MAX_CHAR];` ` ` `for` `(` `int` `i = ` `0` `; i < l; i++)` ` ` `frequency[str.charAt(i) - ` `'a'` `]++;` ` ` `// creating a priority queue with comparator` ` ` `// such that elements in the queue are in` ` ` `// descending order.` ` ` `PriorityQueue<Integer> q = ` `new` `PriorityQueue<>(Collections.reverseOrder());` ` ` `// Push each char frequency into a priority_queue` ` ` `for` `(` `int` `i = ` `0` `; i < MAX_CHAR; i++) {` ` ` `if` `(frequency[i] != ` `0` `)` ` ` `q.add(frequency[i]);` ` ` `}` ` ` `// Removal of K characters` ` ` `while` `(k != ` `0` `) {` ` ` `// Get top element in priority_queue,` ` ` `// remove it. Decrement by 1 and again` ` ` `// push into priority_queue` ` ` `q.add(q.poll() - ` `1` `);` ` ` `k--;` ` ` `}` ` ` `// After removal of K characters find sum` ` ` `// of squares of string Value` ` ` `int` `result = ` `0` `; ` `// Initialize result` ` ` `while` `(!q.isEmpty()) {` ` ` `result += q.peek() * q.poll();` ` ` `}` ` ` `return` `result;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String args[])` ` ` `{` ` ` `String str = ` `"abbccc"` `; ` `// Input 1` ` ` `int` `k = ` `2` `;` ` ` `System.out.println(minStringValue(str, k));` ` ` `str = ` `"aaab"` `; ` `// Input 2` ` ` `k = ` `2` `;` ` ` `System.out.println(minStringValue(str, k));` ` ` `}` `}` `// This code is contributed by Sumit Ghosh` |

## Python 3

`# Python3 program to find min sum of` `# squares of characters after k removals` `from` `queue ` `import` `PriorityQueue` `MAX_CHAR ` `=` `26` `# Main Function to calculate min sum of` `# squares of characters after k removals` `def` `minStringValue(` `str` `, k):` ` ` `l ` `=` `len` `(` `str` `) ` `# find length of string` ` ` `# if K is greater than length of string` ` ` `# so reduced string will become 0` ` ` `if` `(k >` `=` `l):` ` ` `return` `0` ` ` ` ` `# Else find Frequency of each` ` ` `# character and store in an array` ` ` `frequency ` `=` `[` `0` `] ` `*` `MAX_CHAR` ` ` `for` `i ` `in` `range` `(` `0` `, l):` ` ` `frequency[` `ord` `(` `str` `[i]) ` `-` `97` `] ` `+` `=` `1` ` ` `# Push each char frequency negative` ` ` `# into a priority_queue as the queue` ` ` `# by default is minheap` ` ` `q ` `=` `PriorityQueue()` ` ` `for` `i ` `in` `range` `(` `0` `, MAX_CHAR):` ` ` `q.put(` `-` `frequency[i])` ` ` `# Removal of K characters` ` ` `while` `(k > ` `0` `):` ` ` ` ` `# Get top element in priority_queue` ` ` `# multiply it by -1 as temp is negative` ` ` `# remove it. Increment by 1 and again` ` ` `# push into priority_queue` ` ` `temp ` `=` `q.get()` ` ` `temp ` `=` `temp ` `+` `1` ` ` `q.put(temp, temp)` ` ` `k ` `=` `k ` `-` `1` ` ` `# After removal of K characters find` ` ` `# sum of squares of string Value ` ` ` `result ` `=` `0` `; ` `# initialize result` ` ` `while` `not` `q.empty():` ` ` `temp ` `=` `q.get()` ` ` `temp ` `=` `temp ` `*` `(` `-` `1` `)` ` ` `result ` `+` `=` `temp ` `*` `temp` ` ` `return` `result` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `str` `=` `"abbccc"` ` ` `k ` `=` `2` ` ` `print` `(minStringValue(` `str` `, k))` ` ` `str` `=` `"aaab"` ` ` `k ` `=` `2` ` ` `print` `(minStringValue(` `str` `, k))` ` ` `# This code is contributed` `# by Sairahul Jella` |

## C#

`// C# program to find min sum of squares` `// of characters after k removals` `using` `System;` `using` `System.Collections.Generic;` `class` `GFG {` ` ` `static` `readonly` `int` `MAX_CHAR = 26;` ` ` `// Main Function to calculate min sum of` ` ` `// squares of characters after k removals` ` ` `static` `int` `minStringValue(String str, ` `int` `k)` ` ` `{` ` ` `int` `l = str.Length; ` `// find length of string` ` ` `// if K is greater than length of string` ` ` `// so reduced string will become 0` ` ` `if` `(k >= l)` ` ` `return` `0;` ` ` `// Else find Frequency of each character and` ` ` `// store in an array` ` ` `int` `[] frequency = ` `new` `int` `[MAX_CHAR];` ` ` `for` `(` `int` `i = 0; i < l; i++)` ` ` `frequency[str[i] - ` `'a'` `]++;` ` ` `// creating a priority queue with comparator` ` ` `// such that elements in the queue are in` ` ` `// descending order.` ` ` `List<` `int` `> q = ` `new` `List<` `int` `>();` ` ` `// Push each char frequency into a priority_queue` ` ` `for` `(` `int` `i = 0; i < MAX_CHAR; i++)` ` ` `{` ` ` `if` `(frequency[i] != 0)` ` ` `q.Add(frequency[i]);` ` ` `}` ` ` `// Removal of K characters` ` ` `while` `(k != 0)` ` ` `{` ` ` `q.Sort();` ` ` `q.Reverse();` ` ` `// Get top element in priority_queue,` ` ` `// remove it. Decrement by 1 and again` ` ` `// push into priority_queue` ` ` `q.Add(q[0] - 1);` ` ` `q.RemoveAt(0);` ` ` `k--;` ` ` `}` ` ` `// After removal of K characters find sum` ` ` `// of squares of string Value` ` ` `int` `result = 0; ` `// Initialize result` ` ` `while` `(q.Count != 0)` ` ` `{` ` ` `result += q[0] * q[0];` ` ` `q.RemoveAt(0);` ` ` `}` ` ` `return` `result;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `Main(String []args)` ` ` `{` ` ` `String str = ` `"abbccc"` `; ` `// Input 1` ` ` `int` `k = 2;` ` ` `Console.WriteLine(minStringValue(str, k));` ` ` `str = ` `"aaab"` `; ` `// Input 2` ` ` `k = 2;` ` ` `Console.WriteLine(minStringValue(str, k));` ` ` `}` `}` `// This code is contributed by gauravrajput1` |

## Javascript

`<script>` `// JavaScript program to find min sum of squares` `// of characters after k removals` `let MAX_CHAR = 26;` `// Main Function to calculate min sum of` ` ` `// squares of characters after k removals` `function` `minStringValue(str,k)` `{` ` ` `let l = str.length; ` `// find length of string` ` ` ` ` `// if K is greater than length of string` ` ` `// so reduced string will become 0` ` ` `if` `(k >= l)` ` ` `return` `0;` ` ` ` ` `// Else find Frequency of each character and` ` ` `// store in an array` ` ` `let frequency = ` `new` `Array(MAX_CHAR);` ` ` `for` `(let i=0;i<MAX_CHAR;i++)` ` ` `frequency[i]=0;` ` ` `for` `(let i = 0; i < l; i++)` ` ` `frequency[str[i].charCodeAt(0) - ` `'a'` `.charCodeAt(0)]++;` ` ` ` ` `// creating a priority queue with comparator` ` ` `// such that elements in the queue are in` ` ` `// descending order.` ` ` `let q = [];` ` ` ` ` `// Push each char frequency into a priority_queue` ` ` `for` `(let i = 0; i < MAX_CHAR; i++) {` ` ` `if` `(frequency[i] != 0)` ` ` `q.push(frequency[i]);` ` ` `}` ` ` ` ` `q.sort(` `function` `(a,b){` `return` `b-a;});` ` ` ` ` `// Removal of K characters` ` ` `while` `(k != 0) {` ` ` `// Get top element in priority_queue,` ` ` `// remove it. Decrement by 1 and again` ` ` `// push into priority_queue` ` ` `q.push(q.shift() - 1);` ` ` `q.sort(` `function` `(a,b){` `return` `b-a;});` ` ` `k--;` ` ` `}` ` ` ` ` `// After removal of K characters find sum` ` ` `// of squares of string Value` ` ` `let result = 0; ` `// Initialize result` ` ` `while` `(q.length!=0) {` ` ` `result += q[0] * q.shift();` ` ` `}` ` ` ` ` `return` `result;` `}` `// Driver Code` `let str = ` `"abbccc"` `; ` `// Input 1` `let k = 2;` `document.write(minStringValue(str, k)+` `"<br>"` `);` `str = ` `"aaab"` `; ` `// Input 2` `k = 2;` `document.write(minStringValue(str, k)+` `"<br>"` `);` `// This code is contributed by unknown2108` `</script>` |

**Output:**

6 2

**Time Complexity: O(k*logn)**

This article is contributed by **Mr. Somesh Awasthi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.