mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 11:43:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			36 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			36 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>给你一个由字符 <code>'a'</code>、<code>'b'</code>、<code>'c'</code> 组成的字符串 <code>s</code> 和一个非负整数 <code>k</code> 。每分钟,你可以选择取走 <code>s</code> <strong>最左侧</strong> 还是 <strong>最右侧</strong> 的那个字符。</p>
 | 
						||
 | 
						||
<p>你必须取走每种字符 <strong>至少</strong> <code>k</code> 个,返回需要的 <strong>最少</strong> 分钟数;如果无法取到,则返回<em> </em><code>-1</code> 。</p>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>示例 1:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<strong>输入:</strong>s = "aabaaaacaabc", k = 2
 | 
						||
<strong>输出:</strong>8
 | 
						||
<strong>解释:</strong>
 | 
						||
从 s 的左侧取三个字符,现在共取到两个字符 'a' 、一个字符 'b' 。
 | 
						||
从 s 的右侧取五个字符,现在共取到四个字符 'a' 、两个字符 'b' 和两个字符 'c' 。
 | 
						||
共需要 3 + 5 = 8 分钟。
 | 
						||
可以证明需要的最少分钟数是 8 。
 | 
						||
</pre>
 | 
						||
 | 
						||
<p><strong>示例 2:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<strong>输入:</strong>s = "a", k = 1
 | 
						||
<strong>输出:</strong>-1
 | 
						||
<strong>解释:</strong>无法取到一个字符 'b' 或者 'c',所以返回 -1 。
 | 
						||
</pre>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>提示:</strong></p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
 | 
						||
	<li><code>s</code> 仅由字母 <code>'a'</code>、<code>'b'</code>、<code>'c'</code> 组成</li>
 | 
						||
	<li><code>0 <= k <= s.length</code></li>
 | 
						||
</ul>
 |