```
class Solution {
public:
int countBinarySubstrings(string s) {
int res = 0;
for (int i = 0; i < s.size() - 1; ) {
int j = 1;
if (s[i] != s[i + 1]) {
while (i - j >= 0 && i + 1 + j < s.size() && s[i] == s[i - j] && s[i + 1] == s[i + 1 + j]) {
j++;
}
res += j;
}
i += j;
}
return res;
}
};
```