Maximal string problem

for the problem: https://www.interviewbit.com/old/problems/maximal-string/

i have written the following code: https://ide.codingblocks.com/s/576092
but this is failing for larger test cases…why??

I have made the required changes in your code https://ide.codingblocks.com/s/576109
You were adding to the cost even when i was equal to start while doing swap.
So I added permute(B, start+1), for this case.
Also if condition is necessary to avoid TLE.
I used this tc to debug your code
1714254313 ,5 --> 7544331211

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.