Number of Substrings With Only 1s - Problem

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

example_1.py — Basic Case
$ Input: s = "0110111"
› Output: 9
šŸ’” Note: The groups of consecutive 1's are "11" (length 2) and "111" (length 3). Group 1 contributes 2*(2+1)/2 = 3 substrings. Group 2 contributes 3*(3+1)/2 = 6 substrings. Total: 3 + 6 = 9.
example_2.py — All Ones
$ Input: s = "111"
› Output: 6
šŸ’” Note: One group of length 3. Using formula: 3*(3+1)/2 = 6. The substrings are: "1" (appears 3 times), "11" (appears 2 times), "111" (appears 1 time).
example_3.py — No Ones
$ Input: s = "000"
› Output: 0
šŸ’” Note: No substrings contain only 1's, so the answer is 0.

Visualization

Tap to expand
šŸ”¢ Mathematical Pearl CounterString: "0110111" → Count all continuous white pearl segments0110111Group 1: Length = 2Group 2: Length = 3šŸ”“ Group 1 AnalysisLength n = 2 white pearlsFormula: nƗ(n+1)Ć·2 = 2Ɨ3Ć·2 = 3Possible segments:• Single pearl: "1" (2 ways)• Double pearl: "11" (1 way)🟢 Group 2 AnalysisLength n = 3 white pearlsFormula: nƗ(n+1)Ć·2 = 3Ɨ4Ć·2 = 6Possible segments:• Single: "1" (3), Double: "11" (2)• Triple: "111" (1)šŸŽÆ Final CountGroup 1: 3 + Group 2: 6 = 9Total pearl segments with only white pearls: 9
Understanding the Visualization
1
Scan the Necklace
Move through each bead, identifying groups of white pearls
2
Measure Each Group
Count the length of consecutive white pearl segments
3
Apply Magic Formula
For each group of length n, calculate n*(n+1)/2 possible segments
4
Sum All Groups
Add up the contributions from all groups to get the final answer
Key Takeaway
šŸŽÆ Key Insight: Instead of checking every possible substring, we can group consecutive '1's and use the mathematical formula n*(n+1)/2 to instantly calculate the number of valid substrings for each group. This transforms an O(n³) brute force solution into an elegant O(n) mathematical approach!

Time & Space Complexity

Time Complexity
ā±ļø
O(n³)

O(n²) to generate all substrings, O(n) to check each substring

n
2n
⚠ Quadratic Growth
Space Complexity
O(1)

Only using a few variables for counting

n
2n
āœ“ Linear Space

Constraints

  • 1 ≤ s.length ≤ 105
  • s[i] is either '0' or '1'
  • Return the result modulo 109 + 7
Asked in
Amazon 15 Google 12 Microsoft 8 Meta 6
28.4K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
šŸ’” Explanation
AI Ready
šŸ’” Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen