> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimum Number of Changes to Make Binary

> Tested Python solution for LeetCode 2914 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 2914, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/minimum-number-of-changes-to-make-binary-string-beautiful/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2914   # by problem number
lcpy gen -s minimum_number_of_changes_to_make_binary_string_beautiful   # by problem name
```

## Problem

\<p>You are given a \<strong>0-indexed\</strong> binary string \<code>s\</code> having an even length.\</p>

\<p>A string is \<strong>beautiful\</strong> if it's possible to partition it into one or more substrings such that:\</p>

\<ul>
\<li>Each substring has an \<strong>even length\</strong>.\</li>
\<li>Each substring contains \<strong>only\</strong> \<code>1\</code>'s or \<strong>only\</strong> \<code>0\</code>'s.\</li>
\</ul>

\<p>You can change any character in \<code>s\</code> to \<code>0\</code> or \<code>1\</code>.\</p>

\<p>Return \<em>the \<strong>minimum\</strong> number of changes required to make the string \</em>\<code>s\</code> \<em>beautiful\</em>.\</p>

### Examples

```
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.

```
Input: s = "10"
Output: 1
```

**Explanation:** We change s\[1] to 1 to get string "11".
It can be seen that the string "11" is beautiful because we can partition it into "11".
It can be proven that 1 is the minimum number of changes needed to make the string beautiful.

```
Input: s = "0000"
Output: 0
```

**Explanation:** We don't need to make any changes as the string "0000" is beautiful already.

### Constraints

* `2 <= s.length <= 10^5`
* `s` has an even length.
* `s[i]` is either `'0'` or `'1'`.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_number_of_changes_to_make_binary_string_beautiful/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_number_of_changes_to_make_binary_string_beautiful/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def min_changes(self, s: str) -> int:
        # Every substring has even length, so each pair (s[2i], s[2i+1]) must
        # be uniform; aligning each pair to its majority character is optimal.
        changes = 0
        for i in range(0, len(s), 2):
            if s[i] != s[i + 1]:
                changes += 1
        return changes
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.