TLE for test case 1 and 2

Can you suggest how can i optimize the solution

hey @Vishu_1801
after this line a = getP(s);
sort the arrayList and use compareTo function
Collections.sort(a);
for (int i = 0; i < a.size(); i++) {
if (a.get(i).compareTo(s) > 0) {
System.out.println(a.get(i));
}
}
correct code


you can see this

Can you please tell me how this collections.sort() work

method is present in java.util.Collections class. It is used to sort the elements present in the specified list of Collection in ascending order.