-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathP36.java
More file actions
46 lines (37 loc) · 1.22 KB
/
P36.java
File metadata and controls
46 lines (37 loc) · 1.22 KB
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
package stack_and_queue;
import java.util.*;
// Minimum sum of squares of character counts in a given string after removing K characters.
import java.util.HashMap;
public class P36 {
static int minValue(String s, int k) {
// code here
HashMap<Character, Integer> map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
if (!map.containsKey(s.charAt(i)))
map.put(s.charAt(i), 1);
else
map.put(s.charAt(i), map.get(s.charAt(i)) + 1);
}
while (k > 0) {
char ch = ' ';
int max = Integer.MIN_VALUE;
for (Map.Entry<Character, Integer> m : map.entrySet()) {
if (m.getValue() >= max) {
ch = m.getKey();
max = m.getValue();
}
}
map.put(ch, map.get(ch) - 1);
--k;
}
int sum = 0;
@SuppressWarnings("rawtypes")
Iterator i = map.entrySet().iterator();
while (i.hasNext()) {
@SuppressWarnings("rawtypes")
Map.Entry e = (Map.Entry) i.next();
sum += (int) Math.pow((int) e.getValue(), 2);
}
return sum;
}
}