mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-10-23 22:08:58 +08:00
60 lines
3.3 KiB
HTML
60 lines
3.3 KiB
HTML
<p>You are given two strings <code>s</code> and <code>target</code>, both having length <code>n</code>, consisting of lowercase English letters.</p>
|
|
|
|
<p>Return the <strong>lexicographically smallest <span data-keyword="permutation-string">permutation</span></strong> of <code>s</code> that is <strong>strictly</strong> greater than <code>target</code>. If no permutation of <code>s</code> is lexicographically strictly greater than <code>target</code>, return an empty string.</p>
|
|
|
|
<p>A string <code>a</code> is <strong>lexicographically strictly greater </strong>than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, string <code>a</code> has a letter that appears later in the alphabet than the corresponding letter in <code>b</code>.</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", target = "bba"</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">"bca"</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<ul>
|
|
<li>The permutations of <code>s</code> (in lexicographical order) are <code>"abc"</code>, <code>"acb"</code>, <code>"bac"</code>, <code>"bca"</code>, <code>"cab"</code>, and <code>"cba"</code>.</li>
|
|
<li>The lexicographically smallest permutation that is strictly greater than <code>target</code> is <code>"bca"</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 = "leet", target = "code"</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">"eelt"</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<ul>
|
|
<li>The permutations of <code>s</code> (in lexicographical order) are <code>"eelt"</code>, <code>"eetl"</code>, <code>"elet"</code>, <code>"elte"</code>, <code>"etel"</code>, <code>"etle"</code>, <code>"leet"</code>, <code>"lete"</code>, <code>"ltee"</code>, <code>"teel"</code>, <code>"tele"</code>, and <code>"tlee"</code>.</li>
|
|
<li>The lexicographically smallest permutation that is strictly greater than <code>target</code> is <code>"eelt"</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 = "baba", target = "bbaa"</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">""</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<ul>
|
|
<li>The permutations of <code>s</code> (in lexicographical order) are <code>"aabb"</code>, <code>"abab"</code>, <code>"abba"</code>, <code>"baab"</code>, <code>"baba"</code>, and <code>"bbaa"</code>.</li>
|
|
<li>None of them is lexicographically strictly greater than <code>target</code>. Therefore, the answer is <code>""</code>.</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= s.length == target.length <= 300</code></li>
|
|
<li><code>s</code> and <code>target</code> consist of only lowercase English letters.</li>
|
|
</ul>
|