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-deletions-to-make-string-balanced].html

35 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>给你一个字符串&nbsp;<code>s</code>&nbsp;,它仅包含字符&nbsp;<code>'a'</code>&nbsp;<code>'b'</code></p>
<p>你可以删除&nbsp;<code>s</code>&nbsp;中任意数目的字符,使得&nbsp;<code>s</code> <strong>平衡</strong>&nbsp;。当不存在下标对&nbsp;<code>(i,j)</code>&nbsp;满足&nbsp;<code>i &lt; j</code> ,且&nbsp;<code>s[i] = 'b'</code> 的同时&nbsp;<code>s[j]= 'a'</code> ,此时认为 <code>s</code><strong>平衡 </strong>的。</p>
<p>请你返回使 <code>s</code>&nbsp;<strong>平衡</strong>&nbsp;<strong>最少</strong>&nbsp;删除次数。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>s = "aababbab"
<b>输出:</b>2
<b>解释:</b>你可以选择以下任意一种方案:
下标从 0 开始,删除第 2 和第 6 个字符("aa<strong>b</strong>abb<strong>a</strong>b" -&gt; "aaabbb"
下标从 0 开始,删除第 3 和第 6 个字符("aab<strong>a</strong>bb<strong>a</strong>b" -&gt; "aabbbb")。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>s = "bbaaaaabb"
<b>输出:</b>2
<b>解释:</b>唯一的最优解是删除最前面两个字符。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
<li><code>s[i]</code>&nbsp;要么是&nbsp;<code>'a'</code> 要么是&nbsp;<code>'b'</code><strong>&nbsp;</strong>。​</li>
</ul>