#1573

Number of Ways to Split a String

expert · 1080 · lc medium +32 · verified · 34.5% accepted · 768 likes · top 12%

Description

Given a binary string s, count the number of ways to split it into three non-empty parts s1, s2, s3 (with s1 + s2 + s3 = s) such that all three parts contain the same number of '1' characters. Return the count modulo 109 + 7.

Example 1:

Input: s = "10101"
Output: 4
Explanation: There are four ways to split s in 3 parts where each part contain the same number of letters '1'.
"1|010|1"
"1|01|01"
"10|10|1"
"10|1|01"

Example 2:

Input: s = "1001"
Output: 0

Example 3:

Input: s = "0000"
Output: 3
Explanation: There are three ways to split s in 3 parts.
"0|0|00"
"0|00|0"
"00|0|0"

Code

1
2
3