Lexicographically Smallest Beautiful String

A string is beautiful if:

  • It consists of the first k letters of the English lowercase alphabet.
  • It does not contain any substring of length 2 or more which is a palindrome.

You are given a beautiful string s of length n and a positive integer k.

Return the lexicographically smallest string of length n, which is larger than s and is beautiful. If there is no such string, return an empty string.

A string a is lexicographically larger than a string b of the same length if, in the first position where a and b differ, a has a character strictly larger than the corresponding character in b.

Example 1
Inputs = "abcz", k = 26
Output"abda"
The string "abda" is beautiful and lexicographically larger than "abcz", and no smaller valid beautiful string larger than "abcz" exists.
Example 2
Inputs = "dc", k = 4
Output""
There is no beautiful string that is lexicographically larger than "dc".

Constraints

  • 1 <= n == s.length <= 10^5
  • 4 <= k <= 26
  • s is a beautiful string.

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