Leetcode #2914: Minimum Number of Changes to Make Binary String Beautiful
In this guide, we solve Leetcode #2914 Minimum Number of Changes to Make Binary String Beautiful in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
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.
Quick Facts
- Difficulty: Medium
- Premium: No
- Tags: String
Intuition
We need to scan characters while tracking positions or counts.
A simple state machine keeps the logic precise.
Approach
Iterate through the string once and update the state for each character.
Use a map or array if you need fast lookups.
Steps:
- Iterate through characters.
- Maintain necessary state.
- Build or validate the output.
Example
Input: s = "1001"
Output: 2
Explanation: We change s[1] to 1 and s[3] to 0 to get string "1100".
It can be seen that the string "1100" is beautiful because we can partition it into "11|00".
It can be proven that 2 is the minimum number of changes needed to make the string beautiful.
Python Solution
class Solution:
def minChanges(self, s: str) -> int:
return sum(s[i] != s[i - 1] for i in range(1, len(s), 2))
Complexity
The time complexity is , where is the length of the string . The space complexity is .
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.