JavaScript Solution


  • 0
    K
    var countBinarySubstrings = function(s) {
        let queue = [];
        let count = 0;
        let leftLen = 0, rightLen = 0;
        for (let i = 0; i < s.length; i++) {
            let c = s[i];
            if (queue.length === 0) {
                rightLen++;
                leftLen = 0;
            } else {
                let last = queue[queue.length-1];
                if (c !== last) {
                    if (queue[0] === c) {
                        while (queue[0] !== last) {
                            queue.shift();
                        }
                    }
                    leftLen = rightLen;
                    rightLen = 1;
                } else {
                    rightLen++;
                }
            }
            queue.push(c);
            if (leftLen >= rightLen && leftLen !== 0) {
                count++;
            }
        }
        return count;
    }
    

Log in to reply
 

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