Count Ways to Partition Binary String with Exactly Two 0s per Substring

viaLeetCode

Problem: Given a binary string, count the number of ways to partition it into contiguous substrings such that each substring contains exactly two 0s.

Constraints: String length up to 10^5, characters are '0' or '1'.

Example: "100110" -> partitions where each piece has exactly two 0s.

Approach: Locate the positions of all 0s. If the total count of 0s is not divisible by 2, answer is 0. Otherwise, group 0-positions into pairs of two; between consecutive group boundaries, the number of 1s adjacent to a boundary contributes multiplicatively to the number of valid cut points (similar to the "Number of Ways to Split a String" family of problems). Runs in O(n).

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account