mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 11:43:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			40 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			40 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<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>
 |