Skip to main content
Back to problems
#2914
Medium Algorithms

Minimum number of changes to make binary string beautiful

String
76.4% acceptance
Feb 25, 2026
680
117
You are given a 0-indexed binary string s having an even length. A string is beautiful if it's possible to partition it into one or more substrings such that: Each substring has an even length. Each substring contains only 1's or only 0's. You can change any character in s to 0 or 1. Return the minimum number of changes required to make the string s beautiful.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn min_changes(s: String) -> i32 {
    let s = s.as_bytes();
    let n = s.len();
    let mut changes = 0i32;
    let mut i = 0;
    while i < n {
      if s[i] != s[i + 1] {
        changes += 1;
      }
      i += 2;
    }
    changes
  }
}