Lexicographically Smallest Beautiful String - Problem

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.

For example, "abcd" is lexicographically larger than "abcc" because the first position they differ is at the fourth character, and d is greater than c.

Input & Output

Example 1 — Basic Increment
$ Input: s = "abcz", k = 4
Output: "abda"
💡 Note: Cannot increment 'z' (invalid for k=4), backtrack and increment 'c' to 'd', then build smallest valid suffix avoiding palindromes.
Example 2 — Simple Case
$ Input: s = "abc", k = 4
Output: "abd"
💡 Note: Can increment last character 'c' to 'd'. Result "abd" has no palindromic substrings and is beautiful.
Example 3 — No Solution
$ Input: s = "bbab", k = 2
Output: ""
💡 Note: All possible increments lead to palindromes or exceed k=2 limit. No valid next beautiful string exists.

Constraints

  • 1 ≤ n ≤ 105
  • 1 ≤ k ≤ 4
  • s consists of the first k letters of the English lowercase alphabet
  • s is beautiful

Visualization

Tap to expand
Lexicographically Smallest Beautiful String INPUT String s = "abcz" a idx 0 b idx 1 c idx 2 z idx 3 k = 4 (use a, b, c, d) Valid alphabet: a b c d Beautiful String Rules: 1. Only first k letters 2. No palindrome substring (length 2 or more) ALGORITHM STEPS 1 Find rightmost to increment z at idx 3 invalid (z > d) Try idx 2: c can be 'd' 2 Check "abd_" "bd" - not palindrome OK "ad" forms palindrome! Skip 3 Try idx 1: b to 'c' "ac__" - check "ac" Not palindrome - OK! 4 Fill remaining greedily idx 2: try 'a' - "aca" bad idx 2: try 'b' - "acb" OK! idx 3: try 'a' - "ba" OK! abcz --> ac__ --> acba FINAL RESULT Output: "acba" a c changed b filled a filled Verification: 1. Uses only {a,b,c,d}: OK 2. No 2-char palindrome: "ac" OK, "cb" OK, "ba" OK 3. No 3-char palindrome: "acb" OK, "cba" OK 4. "acba" > "abcz": OK Result: "acba" Key Insight: To avoid palindromes, we only need to check the previous 2 characters. A palindrome of length 2 requires s[i] == s[i-1], and length 3 requires s[i] == s[i-2]. Start from rightmost position, increment and fill remaining positions with smallest valid characters (greedy approach). TutorialsPoint - Lexicographically Smallest Beautiful String | Greedy - Increment and Fix
Asked in
Google 25 Meta 18 Apple 12
24.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen