ArrayIndexOutOfBound exception

can you tell me what problem my code is facing

@dd090102_d0e70f0132b9b388 your approach is not totally correct. Instead use this information :
Hacks
To Solve any Problem related to Recursion All you need to do is Break the Problem into 3 important components which are as follows :-

  1. Bigger Problem : The original Problem statement is your bigger problem.
  2. Smaller Problem : In every recursive prblm there exist a prblm statement which you need to achieve in order to fullfill the prblm statement but by considering such a smaller prblm from the bigger prblm is needed which we need to assume that the recursion will work and will give the answer.
  3. Self Work : The amount of work which one should do in order to make the smaller problem your problem.

For e.g…, In order to find the max of any array, three components will be :-
Bigger Problem : To find the max in whole array viz find max in array from index 0 to n - 1.
Smaller Problem : Assume that the recursion works and will find the max of array from index 1 to n - 1.
Self Work : In order to make your smaller prblm your bigger prblm all you need to do is to compare the ans return by assuming the smaller prblm works with the 0th index element and return the max among both of them.

Similarly Classification acc to the Given prblm :-

  • Bigger Problem : To Print all of the possible subsequences.
  • Smaller Problem : Assume the recursion works and will give you ans for subsequences of indices 1 to n - 1 .
    Self Work : In order to make you smaller prblm your problem all you need to work for the 0th index element. Because it could be the part included in the answer or not.

Note : At the end sort the whole array.

Java Code

import java.util.*;

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-- > 0){
            String s = sc.next();
            ArrayList<String> res = allSub(s);
            for(String val:res){
                System.out.println(val);
            }
        }
    }
    public static ArrayList<String> allSub(String str){

        ArrayList<String> rr = subseq(str);

        Collections.sort(rr);
        return rr;

    }

    public static ArrayList<String> subseq(String str) {

        if (str.length() == 0) {

            ArrayList<String> br = new ArrayList<>();
            br.add("");
            return br;
        }

        ArrayList<String> mr = new ArrayList<>();
        ArrayList<String> rr1 = subseq(str.substring(1));

        char ch = str.charAt(0);
        for (String val : rr1) {
            mr.add(val);
            mr.add(ch + val);
        }

        return mr;
    }
}

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.