Created
April 23, 2021 11:33
-
-
Save vrat28/051896f8f09d2a90993a18524467bdfd to your computer and use it in GitHub Desktop.
Count Binary SubStrings(Py)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution(object): | |
def countBinarySubstrings(self, s): | |
ans, prev, cur = 0, 0, 1 | |
for i in xrange(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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment