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.

&nbsp;
Example 1:

Input: s = "00110011"
Output: 6
Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "01".
Notice that some of these substrings repeat and are counted the number of times they occur.
Also, "00110011" is not a valid substring because all the 0's (and 1's) are not grouped together.


Example 2:

Input: s = "10101"
Output: 4
Explanation: There are 4 substrings: "10", "01", "10", "01" that have equal number of consecutive 1's and 0's.


&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 105
	s[i] is either '0' or '1'.


class Solution:
    def countBinarySubstrings(self, s: str) -> int:
        
        # previous continuous occurrence, current continuous occurrence
        pre_cont_occ, cur_cont_occ = 0, 1
        
        # counter for binary substrings with equal 0s and 1s
        counter = 0
        
		# scan each character pair in s
        for idx in range(1, len(s)):
            
            if s[idx] == s[idx-1]:
                
                # update current continuous occurrence
                cur_cont_occ += 1
            
            else:
                # update counter of binary substrings between prevous character group and current character group
                counter += min(pre_cont_occ, cur_cont_occ)

                # update previous as current's continuous occurrence
                pre_cont_occ = cur_cont_occ
                
                # reset current continuous occurrence to 1
                cur_cont_occ = 1
        
        # update for last time
        counter += min(pre_cont_occ, cur_cont_occ)
        
        return counter
class Solution
{
    public int countBinarySubstrings(String s)
    {
        int i , prevRunLength = 0 , curRunLength = 1 , count = 0 ;
        for ( i = 1 ; i < s.length() ; i++ )
        {
            if( s.charAt(i) == s.charAt( i - 1 ) )
            {
                curRunLength++;
            }
            else
            {
                prevRunLength = curRunLength;
                curRunLength = 1;
            }
            if(prevRunLength >= curRunLength)
            {
                count++ ;
            }
        }
        return count ;
    }
}
class Solution {
public:
    int countBinarySubstrings(string s) {
        int prev=0;
        int curr=1;
        int sum=0;

        for(int i=1;i<s.length();i++){
            if(s[i]==s[i-1]){
                curr++;
            }
            else{
                sum+= min(prev, curr);
                prev=curr;
                curr=1;
            }
        }

        sum+= min(prev, curr);
        return sum;
    }
};
/**
 * find all bit switches '01' and '10'. 
 * From each one expand sideways: i goes left, j goes right
 * Until:
 * if '01' -> i,j != 0,1
 * if '10' -> i,j != 1,0
 * and within input boundaries
 */
var countBinarySubstrings = function(s) {
    let i = 0;
    const n = s.length;
    let count = 0;
    while (i < n-1) {
        if (s[i] != s[i+1]) {
            if (s[i] === '0') {
                count += countZeroOnes(s, i, true);
            } else {
                count += countZeroOnes(s, i, false);
            }
            
        }
        i++;
    }
    return count;
    
    // count the number of valid substrings substrings
    function countZeroOnes(s, start, startsWithZero) {
        let count = 0;
        let i = start;
        let j = start+1;
        const n = s.length;
        if (startsWithZero) {
            while(i >= 0 && j < n && s[i] === '0' && s[j] === '1') {
                count++;
                i--;
                j++;
            }
        } else {
            while(i >= 0 && j < n && s[i] === '1' && s[j] === '0') {
                count++;
                i--;
                j++;
            }
        }
        return count;
    }
}
Count Binary Substrings