|
| 1 | +<h2><a href="https://leetcode.com/problems/minimum-string-length-after-balanced-removals">4090. Minimum String Length After Balanced Removals</a></h2><h3>Medium</h3><hr><p>You are given a string <code>s</code> consisting only of the characters <code>'a'</code> and <code>'b'</code>.</p> |
| 2 | + |
| 3 | +<p>You are allowed to repeatedly remove <strong>any <span data-keyword="substring-nonempty">substring</span></strong> where the number of <code>'a'</code> characters is equal to the number of <code>'b'</code> characters. After each removal, the remaining parts of the string are concatenated together without gaps.</p> |
| 4 | + |
| 5 | +<p>Return an integer denoting the <strong>minimum possible length</strong> of the string after performing any number of such operations.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | + |
| 10 | +<div class="example-block"> |
| 11 | +<p><strong>Input:</strong> <span class="example-io">s = <code>"aabbab"</code></span></p> |
| 12 | + |
| 13 | +<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 14 | + |
| 15 | +<p><strong>Explanation:</strong></p> |
| 16 | + |
| 17 | +<p>The substring <code>"aabbab"</code> has three <code>'a'</code> and three <code>'b'</code>. Since their counts are equal, we can remove the entire string directly. The minimum length is 0.</p> |
| 18 | +</div> |
| 19 | + |
| 20 | +<p><strong class="example">Example 2:</strong></p> |
| 21 | + |
| 22 | +<div class="example-block"> |
| 23 | +<p><strong>Input:</strong> <span class="example-io">s = <code>"aaaa"</code></span></p> |
| 24 | + |
| 25 | +<p><strong>Output:</strong> <span class="example-io">4</span></p> |
| 26 | + |
| 27 | +<p><strong>Explanation:</strong></p> |
| 28 | + |
| 29 | +<p>Every substring of <code>"aaaa"</code> contains only <code>'a'</code> characters. No substring can be removed as a result, so the minimum length remains 4.</p> |
| 30 | +</div> |
| 31 | + |
| 32 | +<p><strong class="example">Example 3:</strong></p> |
| 33 | + |
| 34 | +<div class="example-block"> |
| 35 | +<p><strong>Input:</strong> <span class="example-io">s = <code>"aaabb"</code></span></p> |
| 36 | + |
| 37 | +<p><strong>Output:</strong> <span class="example-io">1</span></p> |
| 38 | + |
| 39 | +<p><strong>Explanation:</strong></p> |
| 40 | + |
| 41 | +<p>First, remove the substring <code>"ab"</code>, leaving <code>"aab"</code>. Next, remove the new substring <code>"ab"</code>, leaving <code>"a"</code>. No further removals are possible, so the minimum length is 1.</p> |
| 42 | +</div> |
| 43 | + |
| 44 | +<p> </p> |
| 45 | +<p><strong>Constraints:</strong></p> |
| 46 | + |
| 47 | +<ul> |
| 48 | + <li><code>1 <= s.length <= 10<sup>5</sup></code></li> |
| 49 | + <li><code>s[i]</code> is either <code>'a'</code> or <code>'b'</code>.</li> |
| 50 | +</ul> |
0 commit comments