File tree 1 file changed +5
-3
lines changed
1 file changed +5
-3
lines changed Original file line number Diff line number Diff line change 14
14
'''
15
15
16
16
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Python code begins here - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
17
-
18
17
class Solution :
19
18
def numSub (self , s : str ) - > int :
20
- count = 0
21
- ans = 0
19
+ count = 0 # count the number of consecutive ones
20
+ ans = 0 # variable to store the final answer
22
21
for i in range (len (s )):
23
22
if s [i ] == '1' :
24
23
count += 1
25
24
else :
25
+ # calculate the number of possible substrings that can be formed
26
+ # from the current consecutive ones and add it to the final answer
26
27
ans = (ans + (count * (count + 1 ))// 2 ) % (10 ** 9 + 7 )
27
28
count = 0
29
+ # handle the case when the string ends with consecutive ones
28
30
ans = (ans + (count * (count + 1 ))// 2 ) % (10 ** 9 + 7 )
29
31
return ans
You can’t perform that action at this time.
0 commit comments