class Solution:
    # Count Consecutive Groups (Top Voted), O(n) time and space
    def countBinarySubstrings(self, s: str) -> int:
        s = list(map(len, s.replace('01', '0 1').replace('10', '1 0').split()))
        return sum(min(a, b) for a, b in zip(s, s[1:]))

    # Linear Scan (Solution), O(n) time, O(1) space
    def countBinarySubstrings(self, s: str) -> int:
        ans, prev, cur = 0, 0, 1
        for i in range(1, len(s)):
            if s[i-1] != s[i]:
                ans += min(prev, cur)
                prev, cur = cur, 1
            else:
                cur += 1

        return ans + min(prev, cur)

class Solution:
    # Count Consecutive Groups (Top Voted), O(n) time and space
    def countBinarySubstrings(self, s: str) -> int:
        s = list(map(len, s.replace('01', '0 1').replace('10', '1 0').split()))
        return sum(min(a, b) for a, b in zip(s, s[1:]))

    # Linear Scan (Solution), O(n) time, O(1) space
    def countBinarySubstrings(self, s: str) -> int:
        ans, prev, cur = 0, 0, 1
        for i in range(1, len(s)):
            if s[i-1] != s[i]:
                ans += min(prev, cur)
                prev, cur = cur, 1
            else:
                cur += 1

        return ans + min(prev, cur)

# Count Consecutive Groups (Top Voted), O(n) time and space # Linear Scan (Solution), O(n) time, O(1) space