Binary Substrings

STRING

Problem

Given a binary string s, return the number of non-empty substrings that have the same number of 0's and 1's, and all the 0's and all the 1's in these substrings are grouped consecutively.

Substrings that occur multiple times are counted the number of times they occur.

Examples

countBinarySubstrings("11000110") // returns 5 // Substrings: "1100", "0011", "10", "01", and "10". countBinarySubstrings("100110") // returns 4 // The harmonious designs are: "10", "0011", "10", and "01".
Loading...