All 7 test case passed but in 8th TC TLE coming pls help

import java.util.*;
public class Main {
static ArrayList trickyPermutation(String str) {
if (str.length() == 0) {
ArrayList basecase = new ArrayList();
basecase.add("");
return basecase;
}
char cc = str.charAt(0);
String ros = str.substring(1);
ArrayList recres = trickyPermutation(ros);
ArrayList myres = new ArrayList();
for (String S : recres) {
for (int i = 0; i <= S.length(); i++) {
String val = S.substring(0, i) + cc + S.substring(i);
if (!myres.contains(val))
myres.add(val);

		}
	}
	Collections.sort(myres);
	return myres;

}

public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	String str = sc.next();
	ArrayList<String> res =  trickyPermutation(str);
	for (String S : res) {
		System.out.println(S);
	}
}

}

Instead of adding elements to the arraylist and then sorting it before printing.

convert the string to a char array. Sort the char array. And then just print the permutations. In this you will save a lot of computation. And the permutations will be sorted