Dictionary Order larger problem 2

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
String str = cin.next();
printString(str, β€œβ€, str);
}

public static void printString(String str, String ans, String org) {
	if (str.length() == 0) {
		if (isGreater(ans, org)) {
			System.out.println(ans);
		}
		return;
	}
	for (int i = 0; i < str.length(); i++) {
		char ch = str.charAt(i);
		String ros = str.substring(0, i) + str.substring(i + 1);
		printString(str, ans + ch, org);
	}
}

public static boolean isGreater(String ans, String org) {
	if (ans.length() == 0) {
		return false;
	}
	char ch1 = ans.charAt(0), ch2 = org.charAt(0);
	if (ch1 < ch2) {
		return false;
	} else if(ch1>ch2) {
		return true;
	}
	return isGreater(ans.substring(1), org.substring(1));
}

}============I have corrected code by now its showing tle please help me to make it efficient

hi @AbhishekAhlawat1102
you can use compareTo function
public static void printPermutation(String ques, String ans,String orig) {

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

        if(ans.compareTo(orig) > 0){
            System.out.println(ans+" ");

        }
        return;
    }

    int cnt = 0;
    for (int i = 0; i < ques.length(); i++) {

        char ch = ques.charAt(i);
        String roq = ques.substring(0, i) + ques.substring(i + 1);

        printPermutation(roq, ans + ch,orig);

    }


}

this logic better than recursion or coventional logic in terms of time complexity In Java recursion is fairly expensive compared to iteration (in general) because it requires the allocation of a new stack frame. If you’re using a functional language like C, recursion might be faster. If you’re using an imperative language like Java, iteration is probably faster.

@AbhishekAhlawat1102
please mark your doubt as resolved and rate my work as well.