-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathCount Binary Substrings.py
33 lines (22 loc) · 1.1 KB
/
Count Binary Substrings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Runtime: 133 ms (Top 63.3%) | Memory: 16.65 MB (Top 94.0%)
class Solution:
def countBinarySubstrings(self, s: str) -> int:
# previous continuous occurrence, current continuous occurrence
pre_cont_occ, cur_cont_occ = 0, 1
# counter for binary substrings with equal 0s and 1s
counter = 0
# scan each character pair in s
for idx in range(1, len(s)):
if s[idx] == s[idx-1]:
# update current continuous occurrence
cur_cont_occ += 1
else:
# update counter of binary substrings between prevous character group and current character group
counter += min(pre_cont_occ, cur_cont_occ)
# update previous as current's continuous occurrence
pre_cont_occ = cur_cont_occ
# reset current continuous occurrence to 1
cur_cont_occ = 1
# update for last time
counter += min(pre_cont_occ, cur_cont_occ)
return counter