1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/生成交替二进制字符串的最少操作数 [minimum-changes-to-make-alternating-binary-string].html
2022-03-29 12:43:11 +08:00

38 lines
1.4 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一个仅由字符 <code>'0'</code><code>'1'</code> 组成的字符串 <code>s</code> 。一步操作中,你可以将任一 <code>'0'</code> 变成 <code>'1'</code> ,或者将 <code>'1'</code> 变成 <code>'0'</code></p>
<p><strong>交替字符串</strong> 定义为:如果字符串中不存在相邻两个字符相等的情况,那么该字符串就是交替字符串。例如,字符串 <code>"010"</code> 是交替字符串,而字符串 <code>"0100"</code> 不是。</p>
<p>返回使 <code>s</code> 变成 <strong>交替字符串</strong> 所需的 <strong>最少</strong> 操作数。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>s = "0100"
<strong>输出:</strong>1
<strong>解释:</strong>如果将最后一个字符变为 '1' s 就变成 "0101" ,即符合交替字符串定义。
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>s = "10"
<strong>输出:</strong>0
<strong>解释:</strong>s 已经是交替字符串。
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>s = "1111"
<strong>输出:</strong>2
<strong>解释:</strong>需要 2 步操作得到 "0101" 或 "1010" 。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li>
<li><code>s[i]</code><code>'0'</code><code>'1'</code></li>
</ul>