LeetCode 2914, Medium. Topics: String. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 22 parametrized pytest cases, and a playground notebook:
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
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.
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.
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, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026