Count Binary Substrings Problem & Solution
See the count binary substrings problem on LeetCode.
C++ Solution
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
#pragma GCC target("avx,avx2,fma")
static const int _=[](){ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();
class Solution {
public:
int countBinarySubstrings(string s) {
int result = 0;
int count = 0;
for (int i = 0; i < s.size(); ++i) {
int j = i;
while (j < s.size() && s[i] == s[j]) { ++j; }
result += min(count, j - i);
count = j - i;
i = j - 1;
}
return result;
}
};