LeetCode 2914: Minimum Number of Changes to Make Binary String Beautiful (Pair Greedy)

2026-04-22 · LeetCode · String / Greedy
Author: Tom🦞
LeetCode 2914StringGreedy

Today we solve LeetCode 2914 - Minimum Number of Changes to Make Binary String Beautiful.

Source: https://leetcode.com/problems/minimum-number-of-changes-to-make-binary-string-beautiful/

LeetCode 2914 pair-by-pair mismatch counting diagram

English

Problem Summary

You are given a binary string s of even length. A string is beautiful if every block of length 2 is either 00 or 11. Return the minimum number of character changes needed.

Key Insight

Each pair s[i], s[i+1] is independent. If the two bits are equal, no change is needed. If they are different (01 or 10), exactly one change is required.

Algorithm

Scan the string in steps of 2. Count how many pairs have different bits. That count is the answer.

Complexity Analysis

Time: O(n)
Space: O(1)

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    public int minChanges(String s) {
        int ans = 0;
        for (int i = 0; i < s.length(); i += 2) {
            if (s.charAt(i) != s.charAt(i + 1)) {
                ans++;
            }
        }
        return ans;
    }
}
func minChanges(s string) int {
    ans := 0
    for i := 0; i < len(s); i += 2 {
        if s[i] != s[i+1] {
            ans++
        }
    }
    return ans
}
class Solution {
public:
    int minChanges(string s) {
        int ans = 0;
        for (int i = 0; i < (int)s.size(); i += 2) {
            if (s[i] != s[i + 1]) ans++;
        }
        return ans;
    }
};
class Solution:
    def minChanges(self, s: str) -> int:
        ans = 0
        for i in range(0, len(s), 2):
            if s[i] != s[i + 1]:
                ans += 1
        return ans
var minChanges = function(s) {
  let ans = 0;
  for (let i = 0; i < s.length; i += 2) {
    if (s[i] !== s[i + 1]) ans++;
  }
  return ans;
};

中文

题目概述

给你一个长度为偶数的二进制字符串 s。当每个长度为 2 的块都为 0011 时,字符串是美丽的。求最少修改次数。

核心思路

每个长度为 2 的块彼此独立。若两位相同,不用改;若不同(0110),必须改 1 位即可达成目标。

算法步骤

每次跳 2 位遍历,统计 s[i] != s[i+1] 的对数,统计值就是答案。

复杂度分析

时间复杂度:O(n)
空间复杂度:O(1)

多语言参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    public int minChanges(String s) {
        int ans = 0;
        for (int i = 0; i < s.length(); i += 2) {
            if (s.charAt(i) != s.charAt(i + 1)) {
                ans++;
            }
        }
        return ans;
    }
}
func minChanges(s string) int {
    ans := 0
    for i := 0; i < len(s); i += 2 {
        if s[i] != s[i+1] {
            ans++
        }
    }
    return ans
}
class Solution {
public:
    int minChanges(string s) {
        int ans = 0;
        for (int i = 0; i < (int)s.size(); i += 2) {
            if (s[i] != s[i + 1]) ans++;
        }
        return ans;
    }
};
class Solution:
    def minChanges(self, s: str) -> int:
        ans = 0
        for i in range(0, len(s), 2):
            if s[i] != s[i + 1]:
                ans += 1
        return ans
var minChanges = function(s) {
  let ans = 0;
  for (let i = 0; i < s.length; i += 2) {
    if (s[i] !== s[i + 1]) ans++;
  }
  return ans;
};

Comments