mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
33 lines
1.4 KiB
HTML
33 lines
1.4 KiB
HTML
<p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>.</p>
|
||
|
||
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
|
||
|
||
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
|
||
|
||
<p> </p>
|
||
<p><strong>Example 1:</strong></p>
|
||
|
||
<pre>
|
||
<strong>Input:</strong> s = "aababbab"
|
||
<strong>Output:</strong> 2
|
||
<strong>Explanation:</strong> You can either:
|
||
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
|
||
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
|
||
</pre>
|
||
|
||
<p><strong>Example 2:</strong></p>
|
||
|
||
<pre>
|
||
<strong>Input:</strong> s = "bbaaaaabb"
|
||
<strong>Output:</strong> 2
|
||
<strong>Explanation:</strong> The only solution is to delete the first two characters.
|
||
</pre>
|
||
|
||
<p> </p>
|
||
<p><strong>Constraints:</strong></p>
|
||
|
||
<ul>
|
||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>.</li>
|
||
</ul>
|