Sanket and strings problem 2

Two test cases are passing out of 3. Is it because of the 2 loops or there is some logical error?