-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path696.java
More file actions
45 lines (44 loc) · 1.32 KB
/
696.java
File metadata and controls
45 lines (44 loc) · 1.32 KB
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
39
40
41
42
43
44
45
__________________________________________________________________________________________________
sample 5 ms submission
class Solution {
public int countBinarySubstrings(String s) {
int count = 0;
int count1 = 1;
int count2 = 0;
char[] arr = s.toCharArray();
for(int i = 1; i < arr.length; i++){
if(arr[i] == arr[i - 1]) {
count1++;
if(count2 >= count1) count++;
}
else{
count++;
count2 = count1;
count1 = 1;
}
}
return count;
}
}
__________________________________________________________________________________________________
sample 38216 kb submission
class Solution {
public int countBinarySubstrings(String s) {
int[] groups = new int[s.length()];
int t = 0;
groups[0] = 1;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i-1) != s.charAt(i)) {
groups[++t] = 1;
} else {
groups[t]++;
}
}
int ans = 0;
for (int i = 1; i <= t; i++) {
ans += Math.min(groups[i-1], groups[i]);
}
return ans;
}
}
__________________________________________________________________________________________________