One Round Solution

class Solution:
    def countBinarySubstrings(self, s: str) -> int:
        """
        tc O(N) sc O(1)
        main idea: two pointer - cnt number 0 or 1 grouped consecutively,
                   compare current value's occurance and previous opposite value's occurance  => valid number of substring is min number of consecutive 0s and 1s
        1. start from idex 1, initialize current accumutive occurance as 1 and previous occurance with 0 
        2. check if current value at current index is same as previous idex's value, if yes, keep add current cnt 
        3. otherwise, add min value of both current cnt and previous cnt onto res; assign current cnt to previous cnt, reset current cnt as 1 
        4. when loop ends, compare current and previous cnt again
        
        """
        n = len(s)
        cur, pre = 1, 0 
        res = 0
        for i in range(1,n):
            if s[i] != s[i-1]:
                res += min(pre,cur)
                pre,cur = cur,1
            else:
                cur += 1
        return res + min(pre,cur)

Naive solution

class Solution:
    def countBinarySubstrings(self, s: str) -> int:
        n = len(s)
        cur = 0 
        res = 0
        for i in range(n-1):
            if s[i] != s[i+1]:
                left, right = i,i+1
                res += 1
                while left -1 >= 0 and right+1 < n :
                    if s[left] == s[left-1] and s[right] == s[right+1]:
                        left -= 1 
                        right += 1
                        res += 1  
                    else:
                        break 
        return res