Minimize string value
Submissions: 8627   Accuracy:

22.38%

  Difficulty: Basic   Marks: 1

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 sum of squares of 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^1 + 2^2 = 9.

Input:
The first line of input contains the number T denoting the no of test cases . Then T test cases follow. Each test case contains two lines.The first line of each test case contains a string str. The second line of each test case consist of an integer k .

Output:
The output for each test case will be an integer denoting the min possible value of the string.

Constraints:
1<=T<=100 
1<=str<=50
1<=k<=50

Example:
Input

2
abccc
1
aaab
2
Output
6
2

 

** For More Input/Output Examples Use 'Expected Output' option **

Author: Shubham Joshi 1


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.