-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
696.cpp
54 lines (54 loc) · 1.41 KB
/
696.cpp
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
46
47
48
49
50
51
52
53
54
__________________________________________________________________________________________________
sample 12 ms submission
class Solution {
public:
int countBinarySubstrings(string s) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n=s.length();
if(n==1)return 0;
char curr=s[0];
int i=1;
int cnt=0;
int l=-1,r=-1;
while(i<n)
{
while((i<n)&&(s[i]==curr))i++;
if(i==n)break;
l=i-1;
r=i;
while((l>=0)&&(r<n)&&(s[r]!=curr)&&(s[l]==curr))
{
cnt++;
r++;l--;
}
curr=s[i];
}
return cnt;
}
};
__________________________________________________________________________________________________
sample 12588 kb submission
static int speedup = []() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}();
class Solution {
public:
int countBinarySubstrings(string s) {
int cur=1,pre=0,res=0;
for(int i=1;i<s.size();i++){
if(s[i] == s[i-1]){
cur++;
}else{
res += min(cur,pre);
pre = cur;
cur = 1;
}
}
return res + min(cur,pre);
}
};
__________________________________________________________________________________________________