mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-10-12 08:55:14 +08:00
69 lines
3.0 KiB
HTML
69 lines
3.0 KiB
HTML
<p>You are given a string <code>s</code> consisting of lowercase English letters.</p>
|
|
|
|
<p>You can perform the following operation any number of times (including zero):</p>
|
|
|
|
<ul>
|
|
<li>Remove <strong>any</strong> pair of <strong>adjacent</strong> characters in the string that are <strong>consecutive</strong> in the alphabet, in either order (e.g., <code>'a'</code> and <code>'b'</code>, or <code>'b'</code> and <code>'a'</code>).</li>
|
|
<li>Shift the remaining characters to the left to fill the gap.</li>
|
|
</ul>
|
|
|
|
<p>Return the <strong><span data-keyword="lexicographically-smaller-string">lexicographically smallest</span></strong> string that can be obtained after performing the operations optimally.</p>
|
|
|
|
<p><strong>Note:</strong> Consider the alphabet as circular, thus <code>'a'</code> and <code>'z'</code> are consecutive.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">"a"</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<ul>
|
|
<li>Remove <code>"bc"</code> from the string, leaving <code>"a"</code> as the remaining string.</li>
|
|
<li>No further operations are possible. Thus, the lexicographically smallest string after all possible removals is <code>"a"</code>.</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">s = "bcda"</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">""</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<ul>
|
|
<li><strong></strong>Remove <code>"cd"</code> from the string, leaving <code>"ba"</code> as the remaining string.</li>
|
|
<li>Remove <code>"ba"</code> from the string, leaving <code>""</code> as the remaining string.</li>
|
|
<li>No further operations are possible. Thus, the lexicographically smallest string after all possible removals is <code>""</code>.</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<p><strong class="example">Example 3:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">s = "zdce"</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">"zdce"</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<ul>
|
|
<li>Remove <code>"dc"</code> from the string, leaving <code>"ze"</code> as the remaining string.</li>
|
|
<li>No further operations are possible on <code>"ze"</code>.</li>
|
|
<li>However, since <code>"zdce"</code> is lexicographically smaller than <code>"ze"</code>, the smallest string after all possible removals is <code>"zdce"</code>.</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= s.length <= 250</code></li>
|
|
<li><code>s</code> consists only of lowercase English letters.</li>
|
|
</ul>
|