Skip to content
LC-1513 Medium LeetCode

1513. Number of Substrings With Only 1s

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 48% Topics: Math, String
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

class Solution(object):
    def numSub(self, s):
        """
        :type s: str
        :rtype: int
        """
        MOD = 10**9+7
        result, count = 0, 0
        for c in s:
            count = count+1 if c == '1' else 0
            result = (result+count)%MOD
        return result

Solution from kamyu104/LeetCode-Solutions · MIT