Number of Times Binary String Is Prefix-Aligned LeetCode solution in JavaScript
1var numTimesAllBlue = function (flips) {2const flipped = new Array(flips.length).fill(0);3let prefixAlignedCount = 0;4flips.forEach((i, step) => {5flipped[i - 1] = 1;6if (isPrefixAligned(step)) {7++prefixAlignedCount;8}9});10return prefixAlignedCount;1112function isPrefixAligned(step) {13for (let i = 0; i < flips.length; ++i) {14if ((i < step && flipped[i] === 0) || (i > step && flipped[i] === 1)) {15return false;16}17}18return true;19}20};