You're given a binary string s containing only characters '0' and '1'. Your task is to count all possible substrings that consist entirely of '1's.
A substring is a contiguous sequence of characters within a string. For example, in the string "111", the substrings containing only '1's are: "1" (appears 3 times), "11" (appears 2 times), and "111" (appears 1 time), for a total of 6 substrings.
Since the result can be very large, return the answer modulo 109 + 7.
Example: For s = "0110111", the groups of consecutive '1's are "11" and "111". The first group contributes 3 substrings, the second contributes 6 substrings, totaling 9.
Input & Output
Visualization
Time & Space Complexity
O(n²) to generate all substrings, O(n) to check each substring
Only using a few variables for counting
Constraints
- 1 ⤠s.length ⤠105
-
s[i] is either
'0'or'1' - Return the result modulo 109 + 7