Skip to main content
Back to problems
#1529
Medium Algorithms

Minimum suffix flips

String Greedy
73.9% acceptance
Feb 25, 2026
1069
50
You are given a 0-indexed binary string target of length n. You have another binary string s of length n that is initially set to all zeros. You want to make s equal to target. In one operation, you can pick an index i where 0 <= i < n and flip all bits in the inclusive range [i, n - 1]. Return the minimum number of operations needed to make s equal to target.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn min_flips(target: String) -> i32 {
    let mut ops = 0;
    let mut current = b'0';
    for b in target.bytes() {
      if b != current {
        ops += 1;
        current = b;
      }
    }
    ops
  }
}