Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created July 24, 2018 09:24
Show Gist options
  • Save luojiyin1987/4a9ad9bdc50d394f20244e29c47e9d89 to your computer and use it in GitHub Desktop.
Save luojiyin1987/4a9ad9bdc50d394f20244e29c47e9d89 to your computer and use it in GitHub Desktop.
Count Binary Substrings
class Solution:
def countBinarySubstrings(self, s):
"""
:type s: str
:rtype: int
"""
size = len(s)
cnt = [0, 0]
last = None
ans = 0
for c in s:
c = int(c)
if c != last: cnt[c] = 0
cnt[c] += 1
if cnt[c] <= cnt[1 - c]: ans += 1
last = c
return ans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment