Your bug is due to repeatedly calling strlen(str) in the loop termination condition. strlen has a time complexity of O(n) and calling it everytime makes it O(n*n). Store it as a variable and it will get AC. Link
Why it passes when declared as a local variable is due to -O2 optimization. It is interesting such thing isn’t being done for globally declared variables, this provides good insight on that
Okay so using slrlen() every time was a silly mistake. Thanks for that
But can their be a case when my code gets a TLE because of using a global variable ?
If yes, what is the solution ? should global only be used when it is REALLY required ? and we cant do like “Oh i declare everything global every time?”