-
Notifications
You must be signed in to change notification settings - Fork 2
/
Game-with-String.java
54 lines (43 loc) · 1.26 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
48
49
50
51
52
53
54
//{ 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));
}
}
}
// } Driver Code Ends
// User function Template for Java
class Solution{
static int minValue(String s, int k){
// code here
Map<Character,Integer> map = new HashMap<>();
int res = 0;
for(char c : s.toCharArray()){
map.put(c, map.getOrDefault(c, 0) + 1);
}
PriorityQueue<Integer> q = new PriorityQueue<>(Collections.reverseOrder());
for(int i : map.values()){
q.add(i);
}
while(k > 0){
int cur = q.poll();
q.add(cur - 1);
k--;
}
while(!q.isEmpty()){
int cur = q.poll();
res += (cur * cur);
}
return res;
}
}