I came across the following question,
Find the total number of substrings in a string which contain equal number of 1's and 0's. Also the substring should have consecutive 0's followed by consecutive 1's or vice versa.
For example,
1010 - 10,01,10
1100110- 1100,10,0011,01,10.
My initial idea was to use a n^2 loop to find all substrings and then check if the condition is satisfied. Obviously there must be a better solution to this as I could not pass all cases.
Please suggest ideas to improve on this. Thanks.
0110isn't acceptable; got it. You can divide the string at the middle; one half will be all 0s, and the other will be all 1s.