Problem statement

https://leetcode.com/problems/number-of-substrings-with-only-1s/

Solution

For each group of ones of length k we have k*(k+1)//2 options.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def numSub(self, s):
        ans = 0
        for x, y in groupby(s):
            if x == "1":
                n = len(list(y))
                ans += (n + 1)*n//2
        return ans % (10**9 + 7)