#2663
Lexicographically Smallest Beautiful String
master · 1770 · lc hard +32 · verified · 38.1% accepted · 224 likes · top 17%
Description
A string is beautiful if it uses only the first k lowercase letters and contains no palindromic substring of length 2 or more. Given a beautiful string s of length n and integer k, return the lexicographically smallest beautiful string of the same length that is strictly greater than s, or an empty string if none exists.
Example 1:
Input: s = "abcz", k = 26
Output: "abda"
Explanation: The string "abda" is beautiful and lexicographically larger than the string "abcz".
It can be proven that there is no string that is lexicographically larger than the string "abcz", beautiful, and lexicographically smaller than the string "abda".
Example 2:
Input: s = "dc", k = 4
Output: ""
Explanation: It can be proven that there is no string that is lexicographically larger than the string "dc" and is beautiful.
Code
1
2
3