-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBearAndSteady.java
124 lines (105 loc) · 2.86 KB
/
BearAndSteady.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.io.*;
import java.util.*;
/*
* To execute Java, please define "static void main" on a class
* named Solution.
*
* If you need more classes, simply define them inline.
*/
class BearSteady {
public static int[] counts(String string){
int[] counts = {0,0,0,0}; // A C G T
for(int i = 0; i< string.length(); i++){
switch (string.charAt(i)) {
case 'A': counts[0] = counts[0]+1;break;
case 'C': counts[1] = counts[1]+1;break;
case 'G': counts[2] = counts[2]+1;break;
case 'T': counts[3] = counts[3]+1;break;
}
}
return counts;
}
public static int arrSum(int[] a){
int sum = 0;
for (int s: a){
sum += s;
}
return sum;
}
public static int[] getNeeded(int[] counts, int n){
int[] result = new int[counts.length];
for(int i = 0; i< counts.length; i++){
if(counts[i] > (n/4)){
result[i] = counts[i] - (n/4);
}
else{
result[i] = 0;
}
}
return result;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
String string = in.nextLine();
//System.out.println(string);
int[] counts = counts(string);
//for(int c : counts){
// System.out.println(c);
//}
int[] needed = getNeeded(counts, n);
//for(int c : needed){
// System.out.println(c);
//}
int j = 0;
int i = 0;
int mn = Integer.MAX_VALUE;
int[] curvals = {0,0,0,0};
while(i < n || j < n){
//System.out.println("" + i + ", " + j);
//for(int c : curvals){
//System.out.print(" " + c);
//}
//System.out.println("");
Boolean works = true;
for(int k = 0; k < 4; k++){
if(curvals[k] < needed[k]){
works = false;
}
}
if(works){
mn = Math.min(mn, j-i);
if(mn == 0){
break;
}
//if(i > 0){
switch (string.charAt(i)) {
case 'A': curvals[0] = curvals[0]-1;break;
case 'C': curvals[1] = curvals[1]-1;break;
case 'G': curvals[2] = curvals[2]-1;break;
case 'T': curvals[3] = curvals[3]-1;break;
}
//}
i += 1;
}
else if((!works) && j < n){
switch (string.charAt(j)) {
case 'A': curvals[0] = curvals[0]+1;break;
case 'C': curvals[1] = curvals[1]+1;break;
case 'G': curvals[2] = curvals[2]+1;break;
case 'T': curvals[3] = curvals[3]+1;break;
}
j += 1;
}
else{
break;
}
}
if (mn == Integer.MAX_VALUE){
System.out.println(0);
}
else{
System.out.println(mn);
}
}
}