Problem statement

https://binarysearch.com/problems/Count-Substrings-With-All-1s/

Solution

Equal to Leetcode 1513. Number of Substrings With Only 1s.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def solve(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)