-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay 40
More file actions
24 lines (21 loc) · 711 Bytes
/
Day 40
File metadata and controls
24 lines (21 loc) · 711 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#3234.count-the-number-of-substrings-with-dominant-ones:-
class Solution:
def numberOfSubstrings(self, s: str) -> int:
n = len(s)
pre = [-1] * (n + 1)
for i in range(n):
if i == 0 or s[i - 1] == "0":
pre[i + 1] = i
else:
pre[i + 1] = pre[i]
res = 0
for i in range(1, n + 1):
cnt0 = 1 if s[i - 1] == "0" else 0
j = i
while j > 0 and cnt0 * cnt0 <= n:
cnt1 = (i - pre[j]) - cnt0
if cnt0 * cnt0 <= cnt1:
res += min(j - pre[j], cnt1 - cnt0 * cnt0 + 1)
j = pre[j]
cnt0 += 1
return res