Recursion Dictionary (larger)

Whhy my code is not working? Please Help…
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
String s=sc.next();
System.out.println(findlexico(s));

}
public static ArrayList<String> findlexico(String str) //find the dictionary large
{
    ArrayList<String> res=allSub(str);
   ArrayList<String> mrr= new ArrayList<>();
    int pos= res.indexOf(str);
    for(int i=pos+1;i<res.size();i++)
    {
     String val=res.get(i);
     mrr.add(val);
    }
    return mrr;
    
}

public static ArrayList<String> allSub(String str) //lexicographic order
{

    ArrayList<String> rr1 = combi(str);
    Collections.sort(rr1);
    return rr1;
}

public static ArrayList<String> combi(String str) //find all permutations
{
    if(str.length()==0)
    {
        ArrayList<String> br= new ArrayList<>();
        br.add(" ");
        return br;
    }
    
    char cc=str.charAt(0);
    String ros=str.substring(1);
    
    ArrayList<String> mr= new ArrayList<>();
    ArrayList<String> rr=combi(ros);
    
    for(String item:rr)
    {
        for(int i=0;i<item.length();i++)
        {
            mr.add(item.substring(0,i)+cc+item.substring(i));
        }
    }
    return mr;
}

}

@minal.251298,
You are printing all the possible words. Instead print all the words possible which are in dictionary order larger than the given string.

Can you please identify the Problem in the code…

@minal.251298,
First, you don’t need to generate all the strings possible. Since max length of string is 25, it will give you TLE.
All you need to do is to keep track of the original String too and in the base case just check the answer to be printed must be larger than original String.

	int pos = res.indexOf(str);

This was returning -1 in your code, hence the error in returning all the possible strings.

How can I resolve this error??
Int pos = res.indexOf(str)

@minal.251298,
I have corrected your code: https://ide.codingblocks.com/s/195470
The reason why int pos = res.indexOf(str); wasn’t working because there was an extra " " at the end of every string. Hence the mismatch. I have corrected it, also you need to print the final arraylist instead of returning it. Kindly go through the changes.

Thankyou so much, now I got the solution

@minal.251298,
If your doubt is resolved, kindly mark the doubt as resolved.

1 Like

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.