Trains the technique from
LeetCode 2663Lexicographically Smallest Beautiful StringThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A tape is written over the first k letters of the alphabet. It is tidy when no stretch of two or more of its characters reads the same forwards as backwards, which comes to saying that no character matches the one directly before it and none matches the one two before it.
The tape s is tidy. Return the smallest tidy tape of the same length that is strictly larger than s when compared as text, or the empty string when there is none.
Example 1
Raising the last character from c gives d, which clashes with neither b nor a, so "abd" is the answer.
Example 2
The single character is already the largest of the four letters, so nothing larger exists.
Example 3
The last character rises from b to c, giving "ac".
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def smallest_beautiful_string(s: str, k: int) -> str:public String smallestBeautifulString(String s, int k)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.