Lexicographically Smallest Beautiful String
Real-world version: Next Tidy Tape Over k LettersThe statement here is our own: the same inputs, outputs and technique.
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.
Examples
Example 1
- Input
- s = "abc", k = 4
- Output
- "abd"
Raising the last character from c gives d, which clashes with neither b nor a, so "abd" is the answer.
Example 2
- Input
- s = "d", k = 4
- Output
- ""
The single character is already the largest of the four letters, so nothing larger exists.
Example 3
- Input
- s = "ab", k = 4
- Output
- "ac"
The last character rises from b to c, giving "ac".
Constraints
- 1 <= s.length <= 10^5
- 4 <= k <= 26
- s is written over the first k letters and is tidy
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def smallest_beautiful_string(s: str, k: int) -> str:public String smallestBeautifulString(String s, int k)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Valid Palindrome IIReal-world version: Mosaic Border InspectionEasy
- Longest PalindromeReal-world version: Longest Lintel Row of Carved TilesEasy
- Minimum Number of Pushes to Type Word IReal-world version: Eight-Key Field TerminalEasy
- Largest Odd Number in StringReal-world version: Largest Odd Segment of a SerialEasy