In this program, I'm supposed to count the amount of times the letters 'A','B' and 'C' are in the string and return that as an array. The input is ABBACCCCAC and I'm supposed to get an output of [3, 2, 5] but I'm getting [1, 1, 2, 2, 1, 2, 3, 4, 3, 5]
import java.util.Arrays;
public class HelloWorld{
public static void main(String []args){
String str = "ABBACCCCAC";
int[] arr = new int[str.length()];
int acount = 0, bcount = 0, ccount = 0;
for(int i =0; i<str.length();i++){
if(str.charAt(i) == 'A'){
acount++;
arr[i] = acount;
}
else if(str.charAt(i) == 'B'){
bcount++;
arr[i] = bcount;
}
else if(str.charAt(i) == 'C'){
ccount++;
arr[i] = ccount;
}
}
System.out.print(Arrays.toString(arr));
}
}
System.out.print(Arrays.toString(new int[]{acount, bcount, ccount }));