Gary Lai
Sign in Subscribe

696. Count Binary Substrings

Last updated on  Jan 29, 2023

696. Count Binary Substrings

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
Previous 187. Repeated DNA Sequences
Next 380. Insert Delete GetRandom O(1)
Gary Lai © 2025
  • Sign up
Powered by Ghost