-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
33 lines (31 loc) · 880 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
class Solution {
public int countBinarySubstrings(String s) {
int count = 0,
zeroCount = 0,
oneCount = 0;
char lastCharacter = '*';
for (char c : s.toCharArray()) {
if (c == '0') {
if (lastCharacter != '0') {
zeroCount = 1;
} else {
zeroCount += 1;
}
if (zeroCount <= oneCount) {
count++;
}
} else if (c == '1') {
if (lastCharacter != '1') {
oneCount = 1;
} else {
oneCount += 1;
}
if (oneCount <= zeroCount) {
count++;
}
}
lastCharacter = c;
}
return count;
}
}