696. Count Binary Substrings
First, I count the number of or grouped consecutively.
For example "" will be [, , , ]. Second, for any possible substrings with and grouped consecutively, the number of valid substring will be the minimum number of and .
For example "", will be min(, ) = , ("", "", "") ////////////////////// int countBinarySubstrings(string s) {
int cur = , pre = , res = ;
for (int i = ; i < s.size(); i++) {
if (s[i] == s[i - ]) cur++;
else {
res += min(cur, pre); //主要是这里的技巧,比如"0001111", will bemin(3, 4) = 3
, ("01", "0011", "000111"
)
pre = cur;
cur = ;
}
}
return res + min(cur, pre);
}