# 2663. Lexicographically Smallest Beautiful String

## Description

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.

## Solutions

• class Solution {
public String smallestBeautifulString(String s, int k) {
int n = s.length();
char[] cs = s.toCharArray();
for (int i = n - 1; i >= 0; --i) {
int p = cs[i] - 'a' + 1;
for (int j = p; j < k; ++j) {
char c = (char) ('a' + j);
if ((i > 0 && cs[i - 1] == c) || (i > 1 && cs[i - 2] == c)) {
continue;
}
cs[i] = c;
for (int l = i + 1; l < n; ++l) {
for (int m = 0; m < k; ++m) {
c = (char) ('a' + m);
if ((l > 0 && cs[l - 1] == c) || (l > 1 && cs[l - 2] == c)) {
continue;
}
cs[l] = c;
break;
}
}
return String.valueOf(cs);
}
}
return "";
}
}

• class Solution {
public:
string smallestBeautifulString(string s, int k) {
int n = s.size();
for (int i = n - 1; i >= 0; --i) {
int p = s[i] - 'a' + 1;
for (int j = p; j < k; ++j) {
char c = (char) ('a' + j);
if ((i > 0 && s[i - 1] == c) || (i > 1 && s[i - 2] == c)) {
continue;
}
s[i] = c;
for (int l = i + 1; l < n; ++l) {
for (int m = 0; m < k; ++m) {
c = (char) ('a' + m);
if ((l > 0 && s[l - 1] == c) || (l > 1 && s[l - 2] == c)) {
continue;
}
s[l] = c;
break;
}
}
return s;
}
}
return "";
}
};

• class Solution:
def smallestBeautifulString(self, s: str, k: int) -> str:
n = len(s)
cs = list(s)
for i in range(n - 1, -1, -1):
p = ord(cs[i]) - ord('a') + 1
for j in range(p, k):
c = chr(ord('a') + j)
if (i > 0 and cs[i - 1] == c) or (i > 1 and cs[i - 2] == c):
continue
cs[i] = c
for l in range(i + 1, n):
for m in range(k):
c = chr(ord('a') + m)
if (l > 0 and cs[l - 1] == c) or (l > 1 and cs[l - 2] == c):
continue
cs[l] = c
break
return ''.join(cs)
return ''


• func smallestBeautifulString(s string, k int) string {
cs := []byte(s)
n := len(cs)
for i := n - 1; i >= 0; i-- {
p := int(cs[i] - 'a' + 1)
for j := p; j < k; j++ {
c := byte('a' + j)
if (i > 0 && cs[i-1] == c) || (i > 1 && cs[i-2] == c) {
continue
}
cs[i] = c
for l := i + 1; l < n; l++ {
for m := 0; m < k; m++ {
c = byte('a' + m)
if (l > 0 && cs[l-1] == c) || (l > 1 && cs[l-2] == c) {
continue
}
cs[l] = c
break
}
}
return string(cs)
}
}
return ""
}

• function smallestBeautifulString(s: string, k: number): string {
const cs: string[] = s.split('');
const n = cs.length;
for (let i = n - 1; i >= 0; --i) {
const p = cs[i].charCodeAt(0) - 97 + 1;
for (let j = p; j < k; ++j) {
let c = String.fromCharCode(j + 97);
if ((i > 0 && cs[i - 1] === c) || (i > 1 && cs[i - 2] === c)) {
continue;
}
cs[i] = c;
for (let l = i + 1; l < n; ++l) {
for (let m = 0; m < k; ++m) {
c = String.fromCharCode(m + 97);
if (
(l > 0 && cs[l - 1] === c) ||
(l > 1 && cs[l - 2] === c)
) {
continue;
}
cs[l] = c;
break;
}
}
return cs.join('');
}
}
return '';
}