This is in regards to today's Forethought Future Cup contest.

I tried to hack on this solution because it seems that it's time complexity is O(n^2) where n=10^5, but somehow it takes only 170 ms (on custom tests) to run (worst case).

I am not sure if Codeforces uses some optimisation algorithm for a repetitive function call (String.substr() in this case).

Someone please help me to clarify the doubt.