int countBinarySubstrings(string s) {
if(s.size()<2) return 0;
s += "*";
int x = 0, y = 0, res = 0;
char cx = 0, cy = 0, pre = 0;
for(char &c : s) {
if(c!='0' && c!='1') {
res+=min(x,y);
x=0, y=0;
pre = 0;
continue;
}
if(c!=pre) {
res+=min(x,y);
x=y, y=1;
pre = c;
}
else{
y++;
}
}
return res;
}
No comments:
Post a Comment