-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.java
38 lines (33 loc) · 1.02 KB
/
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
37
38
package _696;
class Solution {
public int countBinarySubstrings(String s) {
int sum = 0;
int len = s.length();
char[] chs = s.toCharArray();
int frontLen = 0;
while (frontLen < len && chs[frontLen] == chs[0]) {
frontLen++;
}
int behindLen;
int i = frontLen;
char c;
while (i < len) {
c = chs[i];
behindLen = 0;
while (i < len && chs[i] == c) {
i++;
behindLen++;
}
sum += Math.min(frontLen, behindLen);
frontLen = behindLen;
}
return sum;
}
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.countBinarySubstrings("00110011"));
System.out.println(solution.countBinarySubstrings("111000011"));
System.out.println(solution.countBinarySubstrings("00110"));
System.out.println(solution.countBinarySubstrings("0"));
}
}