Count Binary Substrings

2017/12/4 posted in  leetcode

class Solution {
public:
    int countBinarySubstrings(string s) {
        vector<int> res;
        int tol = 1;
        for (int i=1; i<=s.length(); i++) {
            if (s[i]==s[i-1]) {
                tol++;
            }else{
                res.push_back(tol);
                tol =1;
            }
        }
        int count = 0;
        for(int i=1, n=res.size(); i<n; ++i){
            count += min(res[i-1], res[i]);
        }
        return count;
    }
};