---
title: "696. Count Binary Substrings"
url: "https://laigary.com/interview/coding/696-count-binary-substrings"
type: "note"
section: "coding"
date: "2023-01-29"
updated: "2023-01-29"
tags: ["String"]
---

# 696. Count Binary Substrings

[696\. Count Binary Substrings](https://leetcode.com/problems/count-binary-substrings/)

```python
class Solution:
    def countBinarySubstrings(self, s: str) -> int:
        groups = [1]
        for i in range(1, len(s)):
            if s[i-1] != s[i]:
                groups.append(1)
            else:
                groups[-1] += 1
        ans = 0
        for i in range(1, len(groups)):
            ans += min(groups[i-1], groups[i])
        return ans

```
