Can u plz help me solving this prblem.I cannot think how to proceed with this ,so that that i can solve it with least TC and clean code
Https://leetcode.com/problems/count-binary-substrings/
Hello
@Somasree
We can convert the string s
into an array groups
that represents the length of same-character contiguous blocks within the string. For example, if s = "110001111000000"
, then groups = [2, 3, 4, 6]
.
For every binary string of the form '0' * k + '1' * k
or '1' * k + '0' * k
, the middle of this string must occur between two groups.
Let’s try to count the number of valid binary strings between groups[i]
and groups[i+1]
. If we have groups[i] = 2, groups[i+1] = 3
, then it represents either "00111"
or "11000"
. We clearly can make min(groups[i], groups[i+1])
valid binary strings within this string. Because the binary digits to the left or right of this string must change at the boundary, our answer can never be larger.
Why there is groups of 2,3,4 , 6 only?
“//For every binary string of the form '0' * k + '1' * k
or '1' * k + '0' * k
, the middle of this string must occur between two groups”- Can u plz elaborate this?
@Somasree we have created the groups according to the string as you can see in the given string example it has 2 ‘1’ character in
the beginning. then 3 ‘0’ then 4 ‘1’ and at last there 6 zeroes in the last then we will apply operation like on this.
ok.UNDERSTOOD.THANK U
Sir why the middle of the string must lie between 2 groups??
@Somasree the middle of the string if not will lie between the 2 groups then it that means that you are going wrong because then it might be possibke that you are somewhere far more then the group then your answer calcuated will not be corrected :
if you are not able to understand the written explanation then dry run the code for you will get it.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.