forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
36 lines (34 loc) · 870 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
private String s;
public int numWays(String s) {
this.s = s;
int cnt = 0;
int n = s.length();
for (int i = 0; i < n; ++i) {
if (s.charAt(i) == '1') {
++cnt;
}
}
int m = cnt % 3;
if (m != 0) {
return 0;
}
final int mod = (int) 1e9 + 7;
if (cnt == 0) {
return (int) (((n - 1L) * (n - 2) / 2) % mod);
}
cnt /= 3;
long i1 = find(cnt), i2 = find(cnt + 1);
long j1 = find(cnt * 2), j2 = find(cnt * 2 + 1);
return (int) ((i2 - i1) * (j2 - j1) % mod);
}
private int find(int x) {
int t = 0;
for (int i = 0;; ++i) {
t += s.charAt(i) == '1' ? 1 : 0;
if (t == x) {
return i;
}
}
}
}