Skip to content

Commit b9937dc

Browse files
Update Substrings_with_1.py
1 parent de1440b commit b9937dc

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

Math/Substrings_with_1.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@
1414
'''
1515

1616
----------------------------------------------------------------------------------------------//Python code begins here--------------------------------------------------------------------------------------------------------------------------------------
17-
1817
class 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

0 commit comments

Comments
 (0)