1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/构成交替字符串需要的最小交换次数 [minimum-number-of-swaps-to-make-the-binary-string-alternating].html
2022-03-29 12:43:11 +08:00

40 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>s</code> ,现需要将其转化为一个 <strong>交替字符串</strong> 。请你计算并返回转化所需的 <strong>最小</strong> 字符交换次数,如果无法完成转化,返回<em> </em><code>-1</code><em> </em></p>
<p><strong>交替字符串</strong> 是指:相邻字符之间不存在相等情况的字符串。例如,字符串 <code>"010"</code><code>"1010"</code> 属于交替字符串,但 <code>"0100"</code> 不是。</p>
<p>任意两个字符都可以进行交换,<strong>不必相邻</strong></p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>s = "111000"
<strong>输出:</strong>1
<strong>解释:</strong>交换位置 1 和 4"1<em><strong>1</strong></em>10<em><strong>0</strong></em>0" -> "1<em><strong>0</strong></em>10<em><strong>1</strong></em>0" ,字符串变为交替字符串。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>s = "010"
<strong>输出:</strong>0
<strong>解释:</strong>字符串已经是交替字符串了,不需要交换。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>s = "1110"
<strong>输出:</strong>-1
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s[i]</code> 的值为 <code>'0'</code><code>'1'</code></li>
</ul>