-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountpair.java
More file actions
76 lines (71 loc) · 2.49 KB
/
Copy pathcountpair.java
File metadata and controls
76 lines (71 loc) · 2.49 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package RandomCF;
import java.util.Scanner;
public class countpair {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int testcases = input.nextInt();
for(int i =0; i< testcases; i++){
int len = input.nextInt();
int maxOpr = input.nextInt();
String str = input.next();
char[] stri = str.toCharArray();
System.out.println(maxBurle(countArr(len, maxOpr, stri), maxOpr));
}
}
public static int[][] countArr(int len, int maxOpr, char[] stri){
int[][] count = new int[2][26];
char[][] alphy = {
{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'},
{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}
};
for(int j = 0; j<len; j++){
for(int k = 0; k<2; k++){
for(int l = 0; l<26; l++){
if(stri[j]==alphy[k][l]){
count[k][l]++;
}
}
}
}
return count;
}
public static int maxBurle(int[][] count, int maxOpr){
int burle = 0;
for(int n = 0; n<26; n++){
if(count[0][n]>count[1][n]){
burle += count[1][n];
if(maxOpr>0){
int needch = (count[0][n]-count[1][n])/2;
if(needch>maxOpr){
burle+=maxOpr;
maxOpr=0;
}else if(needch<maxOpr){
burle+=needch;
maxOpr-=needch;
}else{
burle+=needch;
maxOpr=0;
}
}
}else if(count[0][n]<count[1][n]){
burle += count[0][n];
if(maxOpr>0){
int needch = (count[1][n]-count[0][n])/2;
if(needch>maxOpr){
burle+=maxOpr;
maxOpr=0;
}else if(needch<maxOpr){
burle+=needch;
maxOpr-=needch;
}else{
burle+=needch;
maxOpr=0;
}
}
}else{
burle+=count[0][n];
}
}
return burle;
}
}