2
public int numWays(String s) {4
long one = 0; // to count number of ones5
long mod = 1_000_000_007;6
char[] c = s.toCharArray();7
for (int i = 0; i < c.length; i++) {8
if (c[i] == '1') one++;11
// there will be 3 cases12
// 1.no ones present in the string13
// 2.number of ones present in the string isnt divisible by 3(since we need to cut 3 parts)14
// 3.number of ones divisible by 316
if (one == 0) // case 118
return (int) ((((n - 1) * (n - 2)) / 2) % mod);20
if (one % 3 != 0) // case 2,which means we cant split ones equally23
long ones = one / 3; // number of ones that should be present in each part25
long part1 = 0; // number of ways in which part1 and part2 can be split27
for (int i = 0; i < c.length; i++) {28
if (c[i] == '1') one++;29
if (one == ones) part1++;30
if (one == 2 * ones) part2++;33
return (int) ((part1 * part2) % mod);