Lexicographically Smallest Permutation Greater Than Target

You are given two strings s and target, both having length n, consisting of lowercase English letters.

Return the lexicographically smallest permutation of s that is strictly greater than target. If no permutation of s is lexicographically strictly greater than target, return an empty string.

A string a is lexicographically strictly greater than a string b of the same length if, in the first position where a and b differ, string a has a letter that appears later in the alphabet than the corresponding letter in b.

Example 1
Inputs = "abc", target = "bba"
Output"bca"
The lexicographically smallest permutation of s that is strictly greater than target is "bca".
Example 2
Inputs = "leet", target = "code"
Output"eelt"
The lexicographically smallest permutation of s that is strictly greater than target is "eelt".

Constraints

  • 1 <= s.length == target.length <= 300
  • s and target consist of only lowercase English letters.

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate