Problem Description
Given a binary string s, split s into 3 non-empty parts s1, s2, s3 such that s = s1 + s2 + s3 and each part contains the same number of ones. Return the number of ways to do so modulo 10^9 + 7.
Key Insights
- When the total number of ones in s is not divisible by 3, there is no valid split.
- When the string contains no ones, the answer can be computed using combinatorics, by choosing 2 split points among the (n-1) possible cut positions.
- Otherwise, compute the number of ones each segment must have (totalOnes/3) and then count the number of valid cut positions between segments by counting consecutive zeros adjacent to the boundaries.
Space and Time Complexity
Time Complexity: O(n) where n is the length of the string, since we perform a single pass (or constant number of passes) over s. Space Complexity: O(1) (not including the input string), since we use only a fixed number of variables.
Solution
We first count the total number of ones in s. If totalOnes is 0, then any two cuts chosen among the (n-1) positions yield valid non-empty parts; the result becomes C(n-1, 2).
If totalOnes % 3 != 0, return 0 as the ones cannot be equally distributed.
Otherwise, let onesPerSegment = totalOnes / 3. Traverse the string to locate the positions where the cumulative count of ones equals onesPerSegment (end of the first segment) and 2*onesPerSegment (end of the second segment). Count the number of consecutive positions (i.e. zeros) where a cut can be placed. The product of these two counts gives the total number of ways to split the string.
We then return the answer modulo 10^9+7.