Quote:
I was not able to optimize the code (i.e time limit is exceeding)
With challenge sites, simple minded code is never the solution, you need to be agile in your head. And you need to avoid language features that make your life easier because they cost.
In the inner loop, you create a new string just to count the number of '1', this string is the difference between O(n)=N^3 and O(n)=N^2.
In the inner loop, what is the difference between 2 consecutive strings ? a single char !
Knowing number of '1' and '0' in previous string, do you need to recount everything or just update the number of '1' and '0' for this single char ?
A clever answer should lead you to an O(n)=N^2 solution.