1636. Number of Substrings With Only 1s
My accepted Python solution to LeetCode problem 1636, Number of Substrings With Only 1s, running in 26ms.
- Difficulty: Medium
- Python
- Runtime 26ms
- Memory 17.9MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 26ms, memory 17.9MB, accepted 2025-12-24.
class Solution:
def numSub(self, s: str) -> int:
MOD = 10**9 + 7
result = 0
count = 0
for c in s:
if c == '1':
count += 1
result = (result + count) % MOD
else:
count = 0
return result