String Compression

My code gives incorrect answer for test case 1 and 2. It also gives tle for test case 3. Please let me know my mistake.

import java.util.*;
public class Main {
public static void main(String args[]) {

	Scanner scn = new Scanner(System.in);
	int[] arr = new int[26];
	String str = scn.nextLine();
	int len = str.length();
	for(int i =0;i<len;i++){
		arr[str.charAt(i)-'a']++;
	}
	for(int i =0;i<26;i++){
		if(arr[i]>0){
			int ascii = 97+i;
			char ch = (char)(ascii);
			System.out.print(ch);
			System.out.print(arr[i]);
		}
	}

}

}

@Raunak-Agrawal-1537545869717573,
Input:
aaba
Correct Answer:
a2b1a1
Your answer:
a3b1

Suggested approach:

  1. Take the first character of the present string and store it in say β€˜ch’. Start iterating over the string and obtain the count of this character occurring consecutively from this point.
  2. Obtain the answer for remaining string recursively.
  3. Return ch+countOfCh+recursiveResult .

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.