696. Count Binary Substrings
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2998942dbd680f94579ddaeb2f2f7255.png)
class Solution {
public:
int countBinarySubstrings(string s) {
int count=0,size=0;
char prev=s[0];
vector<int> group;
for(auto x:s){
if(prev==x)
size++;
else{
group.push_back(size);
prev=x;
size=1;
}
}
group.push_back(size);
int len = group.size();
for(int i=1;i<len;i++){
count+=min(group[i-1],group[i]);
}
return count;
}
};
class Solution {
public:
int countBinarySubstrings(string s) {
int prev=0,cur=1,ans=0;
int len = s.size();
for(int i=1;i<len;i++){
if(s[i-1]!=s[i]){
ans+=min(prev,cur);
prev=cur;
cur=1;
}
else
cur++;
}
return ans+min(prev,cur);
}
};