|
| 1 | +<h2><a href="https://leetcode.com/problems/minimum-number-of-changes-to-make-binary-string-beautiful/">2914. Minimum Number of Changes to Make Binary String Beautiful</a></h2><h3>Medium</h3><hr><p>You are given a <strong>0-indexed</strong> binary string <code>s</code> having an even length.</p> |
| 2 | + |
| 3 | +<p>A string is <strong>beautiful</strong> if it's possible to partition it into one or more substrings such that:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>Each substring has an <strong>even length</strong>.</li> |
| 7 | + <li>Each substring contains <strong>only</strong> <code>1</code>'s or <strong>only</strong> <code>0</code>'s.</li> |
| 8 | +</ul> |
| 9 | + |
| 10 | +<p>You can change any character in <code>s</code> to <code>0</code> or <code>1</code>.</p> |
| 11 | + |
| 12 | +<p>Return <em>the <strong>minimum</strong> number of changes required to make the string </em><code>s</code> <em>beautiful</em>.</p> |
| 13 | + |
| 14 | +<p> </p> |
| 15 | +<p><strong class="example">Example 1:</strong></p> |
| 16 | + |
| 17 | +<pre> |
| 18 | +<strong>Input:</strong> s = "1001" |
| 19 | +<strong>Output:</strong> 2 |
| 20 | +<strong>Explanation:</strong> We change s[1] to 1 and s[3] to 0 to get string "1100". |
| 21 | +It can be seen that the string "1100" is beautiful because we can partition it into "11|00". |
| 22 | +It can be proven that 2 is the minimum number of changes needed to make the string beautiful. |
| 23 | +</pre> |
| 24 | + |
| 25 | +<p><strong class="example">Example 2:</strong></p> |
| 26 | + |
| 27 | +<pre> |
| 28 | +<strong>Input:</strong> s = "10" |
| 29 | +<strong>Output:</strong> 1 |
| 30 | +<strong>Explanation:</strong> We change s[1] to 1 to get string "11". |
| 31 | +It can be seen that the string "11" is beautiful because we can partition it into "11". |
| 32 | +It can be proven that 1 is the minimum number of changes needed to make the string beautiful. |
| 33 | +</pre> |
| 34 | + |
| 35 | +<p><strong class="example">Example 3:</strong></p> |
| 36 | + |
| 37 | +<pre> |
| 38 | +<strong>Input:</strong> s = "0000" |
| 39 | +<strong>Output:</strong> 0 |
| 40 | +<strong>Explanation:</strong> We don't need to make any changes as the string "0000" is beautiful already. |
| 41 | +</pre> |
| 42 | + |
| 43 | +<p> </p> |
| 44 | +<p><strong>Constraints:</strong></p> |
| 45 | + |
| 46 | +<ul> |
| 47 | + <li><code>2 <= s.length <= 10<sup>5</sup></code></li> |
| 48 | + <li><code>s</code> has an even length.</li> |
| 49 | + <li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li> |
| 50 | +</ul> |
0 commit comments