-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKGoodWords.java
More file actions
56 lines (43 loc) · 1.25 KB
/
KGoodWords.java
File metadata and controls
56 lines (43 loc) · 1.25 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
47
48
49
50
51
52
53
54
55
56
package problems.codechef;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created by arpit on 14/11/16.
*/
public class KGoodWords {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t,k;
String s[];
char ch[];
t=Integer.parseInt(br.readLine());
while (t-->0){
s=br.readLine().split("\\s");
ch=s[0].toCharArray();
k=Integer.parseInt(s[1]);
System.out.println(solve(ch,k));
}
}
private static long solve(char[] ch, int k) {
int freq[]=new int[26];
int n=ch.length,m;
long ans=Long.MAX_VALUE,count;
for (int i = 0; i < n; i++) {
freq[ch[i]-'a']++;
}
for (int i = 0; i < 26; i++) {
m=freq[i];
if (m>0){
count=0;
for (int j = 0; j < 26; j++) {
if (freq[j]<m)count+=freq[j];
else if (freq[j]>(m+k))
count+=freq[j]-(m+k);
}
ans=Long.min(count,ans);
}
}
return ans;
}
}