-
Notifications
You must be signed in to change notification settings - Fork 25
/
Game_with_String.java
47 lines (43 loc) · 1.06 KB
/
Game_with_String.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// { Driver Code Starts
// Initial Template for Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String args[]) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(in.readLine());
while (t-- > 0) {
String s = in.readLine();
int k = Integer.parseInt(in.readLine());
Solution ob = new Solution();
System.out.println(ob.minValue(s, k));
}
}
}
class Solution {
static int minValue(String s, int k) {
int n = s.length();
if (n == 0)
return 0;
int[] arr = new int[26];
for (char c : s.toCharArray()) {
arr[c - 'a']++;
}
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
for (int num : arr) {
if (num != 0)
pq.add(num);
}
while (!pq.isEmpty() && k > 0) {
int freq = pq.poll();
pq.add(--freq);
k--;
}
int sum = 0;
while (!pq.isEmpty()) {
int freq = pq.poll();
sum += (int) Math.pow(freq, 2);
}
return sum;
}
}