TLE in CHEFSHIP(may cook off)

My code is giving tle and i am unable to figure is passing all the sample test cases.
problem :
My code :
Please help!

Your code has a time complexity of O(N^2) and it cannot pass the given constraints in 1 sec which is why its showing TLE. You need to read the editorial for optimised solution or read others’ approach in problem editorial thread.

1 Like

substr(index,length) in the worst case operates in O(length), thereby giving a complexity of roughly o(n^2) to your approach which will not pass in 1 sec.

1 Like

Thanks! I got it