#1375
Medium Algorithms Number of times binary string is prefix aligned
Array
66.0% acceptance
Feb 25, 2026
968
139
You have a 1-indexed binary string of length n where all the bits are 0 initially. We will flip all the bits of this binary string (i.e., change them from 0 to 1) one by one. You are given a 1-indexed integer array flips where flips[i] indicates that the bit at index flips[i] will be flipped in the ith step.
A binary string is prefix-aligned if, after the ith step, all the bits in the inclusive range [1, i] are ones and all the other bits are zeros.
Return the number of times the binary string is prefix-aligned during the flipping process.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn num_times_all_blue(flips: Vec<i32>) -> i32 {
let mut max_flip = 0i32;
let mut count = 0;
for (i, &f) in flips.iter().enumerate() {
max_flip = max_flip.max(f);
if max_flip == (i + 1) as i32 {
count += 1;
}
}
count
}
}