

方法
- 如果 m 不是 3 的倍数,则不存在符合题目要求的分割 s 的方案,因此返回 0
- 如果 m=0,则字符串 s 中的所有字符都为 0,因此可以在 s 的内部的任何位置进行分割。由于 s 的长度为 n,因此有 n−1 个分割位置,选择 2 个不同的分割位置即可将 s 分成 3 个非空子字符串
- 如果 m>0 ,则每个子字符串都包含 m/3 个字符 1,注意两段位置有多少个空槽可以分割,乘起来
class Solution {
public int numWays(String s) {
int n = s.length();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (s.charAt(i) == '1') {
list.add(i);
}
}
int k = list.size();
if (k % 3 != 0) {
return 0;
}
if (k == 0) {
return (n - 1) * (n - 2) / 2;
}
int idx1 = list.get(k / 3 - 1);
int idx2 = list.get(k / 3);
int idx3 = list.get(k / 3 + k / 3 - 1);
int idx4 = list.get(k / 3 + k / 3);
int slot1 = idx2 - idx1;
int slot2 = idx4 - idx3;
return slot1 * slot2;
}
}