My code:-
import java.util.*;
public class Compressstring
{
static Scanner sc=new Scanner(System.in);
public static void main(String args[])
{
ArrayList<Integer> al=new ArrayList<Integer>();
String s=sc.next();
StringBuilder sb=new StringBuilder();
int k=0;
int j = 0;
int t=0;
while(j<s.length())
{
if(s.indexOf(s.charAt(j))==s.lastIndexOf(s.charAt(j)))
{
sb.append(s.charAt(j));
j++;
}
else
{
sb.append(s.charAt(j));
j=s.lastIndexOf(s.charAt(j))+1;
}
}
for (int i = 0; i < sb.length(); i++)
{
al.add(count(sb.charAt(i),s));
}
int l=sb.length();
for (int i = 0; i < l; i++)
{
if(al.get(i)>1)
{
sb.insert(i+1+t,al.get(i));
t++;
}
}
System.out.println(sb);
}
public static int count(char c,String s)
{
int cnt=0;
for (int j =0 ; j <s.length(); j++)
{
if(s.charAt(j)==c)
cnt++;
}
return cnt;
}
}