Problem Description
Given a string s of length n that is already beautiful (it only contains the first k letters of the English lowercase alphabet and has no palindromic substring of length ≥ 2) along with an integer k, return the lexicographically smallest beautiful string of length n which is strictly larger than s. If no such string exists, return an empty string.
A string is beautiful if:
- It consists only of the first k letters (from 'a' to the k-th letter).
- It does not contain any substring (of length 2 or more) that is a palindrome. In practice, this means that no two adjacent characters can be equal (avoiding palindromic substrings of length 2) and any pattern x ? x (for length 3) is forbidden.
Key Insights
- We need to find the next lexicographically greater string than s while maintaining the beauty constraint.
- The beauty constraint can be enforced by ensuring that:
- For any position i, result[i] ≠ result[i-1] (avoids palindromic substrings of length 2).
- For any position i ≥ 2, result[i] ≠ result[i-2] (avoids palindromic substrings of length 3).
- We use a backtracking/greedy approach:
- Traverse s from right to left and try to increment the character at a given position.
- Once a valid increment is found, fill the positions to the right with the smallest valid characters.
- At each step, only characters from 'a' to ('a' + k - 1) are allowed.
Space and Time Complexity
Time Complexity: O(n * k) in the worst case, where n is the length of the string and k is the number of valid letters. Space Complexity: O(n), needed for storing the result string.
Solution
We approach the problem by iterating from the end of the given string s. For each index, try to replace the current character with the next available character (smaller than the maximum allowed letter) that still keeps the string beautiful up to that point. Once a replacement is made, greedily fill the rest of the positions with the smallest valid character that maintains the beauty constraints. If no valid replacement can be found at any position, return an empty string.
The main data structure used is a mutable character list for easy modifications. The algorithm ensures that at every position, the two constraints (adjacent and one-gap) are checked to maintain a “beautiful” string. This backtracking (or reverse greedy) technique ensures that we find the smallest lexicographic string strictly greater than s.