class CountOccurrences {
printOccurrences("AAAABBCCCDAA");
}
public static void printOccurrences
(String str
) { int len = str.length();
char[] arr = str.toCharArray();
char contChar = arr[0];
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (contChar == arr[i]) {
count++;
if (i + 1 >= arr.length) {
sb.append(count).append(contChar);
}
} else {
sb.append(count).append(contChar);
contChar = arr[i];
count = 0;
i--;
}
}
System.
out.
println(sb.
toString()); }
}
Y2xhc3MgQ291bnRPY2N1cnJlbmNlcyB7CiAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgdGhyb3dzIGphdmEubGFuZy5FeGNlcHRpb24gewogICAgcHJpbnRPY2N1cnJlbmNlcygiQUFBQUJCQ0NDREFBIik7CiAgfQoKICBwdWJsaWMgc3RhdGljIHZvaWQgcHJpbnRPY2N1cnJlbmNlcyhTdHJpbmcgc3RyKSB7CiAgICBpbnQgbGVuID0gc3RyLmxlbmd0aCgpOwogICAgY2hhcltdIGFyciA9IHN0ci50b0NoYXJBcnJheSgpOwogICAgY2hhciBjb250Q2hhciA9IGFyclswXTsKICAgIGludCBjb3VudCA9IDA7CiAgICBTdHJpbmdCdWZmZXIgc2IgPSBuZXcgU3RyaW5nQnVmZmVyKCk7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IGFyci5sZW5ndGg7IGkrKykgewogICAgICBpZiAoY29udENoYXIgPT0gYXJyW2ldKSB7CiAgICAgICAgY291bnQrKzsKICAgICAgICBpZiAoaSArIDEgPj0gYXJyLmxlbmd0aCkgewogICAgICAgICAgc2IuYXBwZW5kKGNvdW50KS5hcHBlbmQoY29udENoYXIpOwogICAgICAgIH0KICAgICAgfSBlbHNlIHsKICAgICAgICBzYi5hcHBlbmQoY291bnQpLmFwcGVuZChjb250Q2hhcik7CiAgICAgICAgY29udENoYXIgPSBhcnJbaV07CiAgICAgICAgY291bnQgPSAwOwogICAgICAgIGktLTsKICAgICAgfQogICAgfQogICAgU3lzdGVtLm91dC5wcmludGxuKHNiLnRvU3RyaW5nKCkpOwogIH0KfQo=