C++, O(n)


  • 0
    Z
        int countBinarySubstrings(string s) {
            int l1 = 1, l2 = 0, cnt = 0;
            char curr = s[0];
            for(int i = 1; i < s.size(); ++i) {
                if(s[i] == curr) {
                    ++l1;
                } else {
                    cnt += min(l1, l2);
                    l2 = l1;
                    l1 = 1;
                    curr = s[i];
                }
            }
            return cnt + min(l1, l2);
        }
    

Log in to reply
 

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