DEV Community

Discussion on: Solution: Count Binary Substrings

Collapse
 
malikmoss profile image
malikmoss

I'm still confused as to why we wouldn't start iterating at index 0. Wouldn't that be the first index to start from while comparing it to s[i] - 1? But I appreciate your explanation and solutions!

Collapse
 
seanpgallivan profile image
seanpgallivan

It's because we're comparing s[i] to s[i-1], not s[i] - 1. At i = 0, s[i-1] would be undefined, so we can't find a match until i = 1.

Thinking about it a different way, any matching substring has to be of even length (since it has an equal amount of 0's and 1's), so the first possible matching substring is when i = 1.

Collapse
 
malikmoss profile image
malikmoss

It just clicked. Thanks alot!

Thread Thread
 
seanpgallivan profile image
seanpgallivan

You're welcome!