TRICKY PERMUTATIONS : time limit

How to optimize the code ?

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

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

        boolean flag = true;

        for (int j = i + 1; j < ques.length(); j++) {
            if (ques.charAt(j) == ch) {
                flag = false;
            }
        }

        if (flag)
            permutationNoDuplicates(roq, ans + ch);

Don’t make any call if after ch point str contains ch again

Also I am using .compareTo function to arrange them in lexicographical order but I am still facing some errors.
Please look into this function as well.
The above code for permutations works alright.
Thanks for a shorter approach !!

Okay , I got the approach but I am facing time-limit in just one test case.
How can I optimize the code.

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.