Not accepting the code for recursion dictionary

import java.util.Scanner;

public class lexCom
{
public static void lexo(String str,String ans,String target)
{
if(str.length()==0)
{
int x=ans.compareTo(target);
if(x>0)
System.out.println(ans);

        return;
    }
    for(int i=0;i<str.length();i++)
    {
        char cc=str.charAt(i);
        String ros=str.substring(0,i)+str.substring(i+1);
        lexo(ros, ans+cc,target);
    }
}
public static void main(String[] args)
{
    Scanner sc=new Scanner(System.in);
    String str=sc.next();
    
    lexo(str,"",str);
}

}

this is the full code and is running on netbeans.

the answer is correct but is not in dictionary order. how to change the order before printing?

pass the sorted the string

hope this will help u

1 Like

but dont change the origianl string