Why is this code wrong , its properly working on ecllipse

Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
String forSpace=sc.nextLine();

	for(int i=1 ; i<=N ; i++)
	{
		String s=sc.nextLine();
	
		int arr[]=new int[s.length()];
		
		for(int j=0 ; j<s.length(); j++)
		{
			arr[j]=(int)s.charAt(j);
		}
		
		Arrays.sort(arr);
		
		String so="";
			
		for(int j=0 ; j<arr.length ; j++)
		{
			so+=(char)arr[j];
		}
				
	
		System.out.println("");

		ess(so , "");
		

		
	
	}
}



static void ess(String s , String a)
{
	if(s.length()==0)
	{

// System.out.println(a);
return;
}

	String sb=s.substring(1);
	char cc=s.charAt(0);
	

	System.out.println(a+cc);
	ess(sb , a+cc);
	
	ess(sb , a);

@Himanshu-Jhawar-2273952536067590 Bro, So what you have done is you have sorted the input string on the basis of characters.

Now for string “bac”

Expected :-
a
ac
b
ba
bac
bc
c

Your output: -
a
ab
abc
ac
b
bc
c

You changed the actual input ordering, so correct this.

If your doubt is cleared mark it resolved and rate full else lemme know your query bro!

import java.util.*; public class Main { static ArrayList al = new ArrayList<>(); public static void main(String args[]) { Scanner sc=new Scanner(System.in); int N=sc.nextInt(); String space=sc.nextLine(); for(int j=1 ; j<=N ; j++) { String s=sc.nextLine(); subseq(s , “”); System.out.println(""); Collections.sort(al) ; for(int i=1 ; i<al.size() ; i++) { if(!al.get(i).equals(al.get(i-1))) System.out.println(al.get(i-1)); } if( al.get(al.size()-1) != al.get(al.size()-2) ) System.out.println(al.get(al.size()-1)); } } static void subseq(String s , String ans) { if(s.length()==0) { return; } String sb=s.substring(1); char c=s.charAt(0); al.add(ans+c); subseq(sb , ans+c); subseq(sb , ans); } }

@Himanshu-Jhawar-2273952536067590 Bro no need to complicate the code. The simple thing would be to build the answer while building stack and storing the subs only when base case is hit.Even there is no need to print empty string separately there.

Here refer this code, and bro if you have any problem lemme know:-

import java.util.*;
public class Main {

//List to store subsequences
public static ArrayList<String> list = null;

//Work is done while building recursion stack
public static void printAllSS(String s, String sub) {
	//Will add even empty string no need to print it separately.
	if(s.length() == 0) {
		list.add(sub);
		return;
	}

	char cc = s.charAt(0);
	//Inclusion - Exclusion
	printAllSS(s.substring(1), sub);
	printAllSS(s.substring(1), sub + cc);
}
public static void main(String args[]) {
	Scanner s = new Scanner(System.in);

	int n = s.nextInt();
	while(n-- != 0) {
		String str = s.next();
		list = new ArrayList<>();
		printAllSS(str, "");
		Collections.sort(list);
		for(String el : list) {
			System.out.println(el);
		}
	}
}

}

If your doubt is cleared mark it resolved. I recommend you to make recursion tree for this code.