Ctrl K

Three Parts, Equal Ones

Medium
You are given a binary string bits. Cut it into three consecutive non-empty parts so that all three parts contain the same number of '1' characters. Return the number of ways to do this. Two ways differ if at least one cut is in a different place. The answer can be large, so return it modulo 10⁹ + 7.
Example 1
bits='1010010' → 6

There are three ones, so each part needs exactly one. The first cut can go after position 0 or 1 and the second after position 2, 3 or 4: 2 · 3 = 6 ways.

Example 2
bits='00000' → 6

There are no ones, so any cut into three non-empty parts works: two of the four gaps, C(4, 2) = 6.

Example 3
bits='1101' → 2

The valid cuts are "1|1|01" and "1|10|1".

Constraints
  • 3 ≤ bits.length ≤ 10⁵
  • bits[i] is '0' or '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='1010010'
Expected
6

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