Strings-String Compression

giving last test case wrong and second last to be run time error

do a dry run for your code on aaaaassssddddffaaagggssfffgzx

actually it is a simple problem we dont require a boolean array…just iterarte over the string and print the necessary output

let say if the input is aaaaassssddddffaaagggssfffgzx

then the output should be a5s4d4f2a3g3s2f3gzx

yes and once check it with your code…

as you said i did , plss check and tell me what is error in it

you code is absolutely correct just one minor change:

import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();
		
		int n = s.length();
		
		ArrayList<String> list = new ArrayList<String>();
		int i =0;
		while(i<s.length())
		{
			int count=0;
			char x=s.charAt(i);
			while(i<s.length()&&s.charAt(i)==x) {//check that i should not exceed the length of the string
				count++;
				i++;
			}
			
			
			String z= String.valueOf(x);
			list.add(z);
			if(count>1) {
			    String r = String.valueOf(count);
			    list.add(r);
            }
		}

		for(int j =0;j<list.size();j++)
		{
			System.out.print(list.get(j));
		}

    }
}

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.