Skip to main content

Lexicographically Smallest Beautiful String - Video Solutions

HardStringGreedy

Leetcode Weekly contest 343 - Hard - Lexicographically Smallest Beautiful String

Prakhar Agrawal
16:551,358 views
6 video solutions available

Lexicographically Smallest Beautiful String - Video Solution

Watch 6 video solutions for Lexicographically Smallest Beautiful String, a hard level problem involving String, Greedy. This walkthrough by Prakhar Agrawal has 1,358 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

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.

 

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.

 

Constraints:

  • 1 <= n == s.length <= 105
  • 4 <= k <= 26
  • s is a beautiful string.
Read full problem with examples

Approach Overview

Problem Overview: You are given a string s and an integer k representing the first k lowercase letters ('a' to 'a' + k - 1). A string is beautiful if no character equals the previous one or the one two positions before it. The task is to find the lexicographically smallest beautiful string strictly greater than s.

Approach 1: Backtrack and Increment (O(n * k) time, O(1) space)

This method treats the string like a base-k number and increments it from right to left. Start from the last position and try increasing the current character. After each increment, check whether it violates the beauty rule (s[i] != s[i-1] and s[i] != s[i-2]). If the character is valid, rebuild the suffix greedily with the smallest valid characters that keep the string beautiful. If no valid increment exists at a position, move left and continue backtracking.

The key insight: once you increase a character at position i, the prefix becomes fixed. The suffix can then be rebuilt with the smallest valid characters to maintain lexicographic minimality. This approach simulates controlled backtracking while preserving the lexicographically smallest valid continuation. It relies heavily on simple character checks and sequential iteration, making it efficient for constraints typically seen in string problems.

Approach 2: Greedy Character Replacement (O(n * k) time, O(1) space)

The greedy strategy scans the string from right to left and attempts to replace each character with the next valid option. For index i, iterate characters from s[i] + 1 to 'a' + k - 1. Choose the first character that doesn't match the previous two characters. Once a valid replacement is found, rebuild the remaining suffix by picking the smallest valid character at each step.

This works because lexicographic order is determined by the earliest differing character. Increasing the rightmost possible index ensures the smallest overall increase. The suffix reconstruction step uses a greedy rule: always choose the smallest character that doesn't conflict with the previous two positions. The approach combines simple iteration with constraints typical in greedy algorithms and local validation rules from string manipulation.

Recommended for interviews: The greedy right-to-left increment approach is what most interviewers expect. It demonstrates understanding of lexicographic ordering, constraint validation, and suffix reconstruction. Mentioning the backtracking interpretation shows deeper reasoning, but implementing the greedy increment + rebuild pattern proves strong problem‑solving skills.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Backtrack and IncrementO(n * k)O(1)Useful for understanding the search space and how lexicographic increments propagate when constraints break.
Greedy Character ReplacementO(n * k)O(1)Preferred solution in interviews. Efficiently finds the next valid string by incrementing from the right and rebuilding the suffix.