1513. Number of Substrings With Only 1s

1513. Number of Substrings With Only 1s
Photo by NASA / Unsplash

Leetcode Daily again, this problem was pretty strightforward this time as well.

Given a binary string s, return the number of substrings with all characters 1's. Since the answer may be too large, return it modulo 10^(9) + 7.

A simple and short description, nothing too fancy just count the substrings that only have characters 1 with the given string.

Though the main issue I had was READING the problem.

First time I miss was:

with all characters 1

I thought it needs to include 1s, so my math was pretty off from that and it took a bit for me to read

also I mess up on not reading the last part correctly, I thought it was modulo of 10^(9) * 7

Here is the solution code very straight forward:

class Solution:
    def numSub(self, s: str) -> int:
        ans = 0
        count = 0
        MOD = 7 + (10 ** 9)
        for i in range(len(s)):
            c = s[i]
            if c == '1':
                count = count + 1
                ans = (ans + count) % MOD
            else:
                count = 0
        return ans