I am getting TLE in last testcase

You can use next_permutation in built to make all permutations.

is it available in java

I think it’s not in java. It is in c++