Ctrl K

Pieces Made of Ones Only

Medium
You are given a binary string bits. Count its non-empty contiguous pieces (substrings) made only of the character 1. Equal pieces at different positions are counted separately. The answer can be large, so return it modulo 10⁹ + 7.
Example 1
bits='11011' → 6

Each of the two "11" blocks holds three such pieces: "1", "1" and "11". 6 in total.

Example 2
bits='000' → 0

There are no ones, so the answer is 0.

Example 3
bits='0111' → 6

From "111": three pieces of length 1, two of length 2 and one of length 3, so 6.

Constraints
  • 1 ≤ len(bits) ≤ 10⁵
  • bits contains only 0 and 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNumber in Roman Numerals MediumMultiplying Digit Strings MediumFraction as a Decimal MediumFour-Operation Calculator MediumEvery Bracketing Result Medium
loading Python…
Input
bits='11011'
Expected
6

Run checks the first 3 tests in your browser. Submit checks all 31 tests on the server, hidden ones included.