Solution 1

  • TimeO(n)
  • SpaceO(n)

where, n is the length of s

Python · 2026-02-19
class Solution:
    '''
    Time Complexity: O(n)
    Space Complexity: O(n)
    where, n is the length of s
    '''
    def countBinarySubstrings(self, s: str) -> int:
        r = 0
        count = 0
        prevC = 0
        while r < len(s):
            check = s[r]
            l = r
            while l < len(s) and s[l] == check:
                l += 1
            count += min(l-r, prevC)
            prevC = l-r
            r = l
        return count
Leet Code/python.py · L5358–5377