File tree Expand file tree Collapse file tree 1 file changed +5
-3
lines changed Expand file tree Collapse file tree 1 file changed +5
-3
lines changed Original file line number Diff line number Diff line change 1414'''
1515
1616- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Python code begins here - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
17-
1817class Solution :
1918 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
2221 for i in range (len (s )):
2322 if s [i ] == '1' :
2423 count += 1
2524 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
2627 ans = (ans + (count * (count + 1 ))// 2 ) % (10 ** 9 + 7 )
2728 count = 0
29+ # handle the case when the string ends with consecutive ones
2830 ans = (ans + (count * (count + 1 ))// 2 ) % (10 ** 9 + 7 )
2931 return ans
You can’t perform that action at this time.
0 commit comments