Runtime error due to line 18

In my code in line 18 how can I replace ans.substring(2) this line gives runtime error probably because if length of substring is 2 then it cannot access other characters

@tanjuljain19,
Take two strings initially, one the input string and one empty string for the answer.

If the character is ‘p’ check for the next character. If it is ‘i’ then update then unprocessed string and increment the pointer by 2.

Else, just copy and concatenate the processed character to the unprocessed string and increment the pointer by 1.

You can try using stringbuilder in this question. Also you can share your code if the error persists

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.