C++ O(n) time


  • 0
    class Solution {
    public:
        int countBinarySubstrings(string s) {
            int pre = 0, cur = 1, ans = 0;
            for (int i = 1; i < s.size(); ++i) {
                if (s[i] == s[i - 1]) {
                    cur++;
                } else {
                    pre = cur;
                    cur = 1;
                }
                if (pre >= cur) {
                    ans++;
                }
            }
            return ans;
        }
    };
    

Log in to reply
 

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