C++ O(n)time (1) Simple code


  • 0
    J
    class Solution {
    public:
        int countBinarySubstrings(string s) {
            int res = 0;
            for (int i = 0; i < s.size() - 1; ) {
                int j = 1;
                if (s[i] != s[i + 1]) {
                    while (i - j >= 0 && i + 1 + j < s.size() && s[i] == s[i - j] && s[i + 1] == s[i + 1 + j]) {
                        j++;
                    }
                    res += j;
                }
                i += j;
            }
            return res;
        }
    };
    

Log in to reply
 

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