LeetCode solutions

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

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 26ms, memory 17.9MB, accepted 2025-12-24.

python
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

Source