My accepted C++ solution


  • 0
    C
    public:
        int countBinarySubstrings(string s) {
            int headflag = s[0];
            int headcount = 1;
            int tailcount = 0;
            int res = 0;
            for(int i = 1; i < s.size(); i++){
                if(s[i] == headflag){
                    if(tailcount==0){
                        headcount++;
                    }
                    else{
                        res+=min(headcount, tailcount);
                        headcount = tailcount;
                        tailcount = 1;
                        headflag = s[i-1];
                    }
                }
                else{
                    tailcount++;
                }
            }
            res+=min(headcount, tailcount);
            return res;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.