LeetCode Entry

1758. Minimum Changes To Make Alternating Binary String

05.03.2026 easy 2026 kotlin rust

Min flips to make bits alterating

1758. Minimum Changes To Make Alternating Binary String easy blog post substack youtube

4823be9e-b115-47a6-8a53-ebca54caf6c0 (1).webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1288

Problem TLDR

Min flips to make bits alterating #easy

Intuition

Check two targets: 01-type and 10-type.

Approach

  • we can count a balance: balance = correct-wrong , L=C+W, L- B =(C+W)-(C-W)=2W, W=(L- B )/2
  • (i + s[i]) %2 is an even-odd match

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(1)\)

Code

// 16ms
    fun minOperations(s: String) = s.length/2-
    abs(s.indices.sumOf { 1-2*((it+s[it].code)%2) })/2
// 0ms
    pub fn min_operations(s: String) -> i32 {
        s.len()as i32/2 - s.bytes().enumerate().map(|(i,b)|(1-2*((i as u8^b)&1)as i32)).sum::<i32>().abs()/2
    }