DEVCLASS TLE

I solved this problem by swapping the first character that is not at its proper place. Here is my solution:

http://www.codechef.com/viewsolution/6495547

How can I optimize this code so as to remove TLE?