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 lengthn, which is larger thansand isbeautiful. 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: s ="abcz", k =26Output: "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".
Input: s ="dc", k =4Output: ""Explanation: It can be proven that there is no string that is lexicographically larger than the string "dc" and is beautiful.
To find the lexicographically smallest beautiful string greater than s, we try to increment the string from the end, ensuring that after each change, the string remains beautiful (no palindromic substrings of length 2 or 3). After incrementing, we greedily fill the rest with the smallest possible valid characters.
classSolution {
public: string smallestBeautifulString(string s, int k) {
int n = s.size();
for (int i = n-1; i >=0; --i) {
for (char c = s[i]+1; c <'a'+k; ++c) {
if ((i >0&& c == s[i-1]) || (i >1&& c == s[i-2])) continue;
string t = s.substr(0, i) + c;
for (int j = i+1; j < n; ++j) {
for (char d ='a'; d <'a'+k; ++d) {
if ((j >0&& d == t[j-1]) || (j >1&& d == t[j-2])) continue;
t += d;
break;
}
}
return t;
}
}
return"";
}
};
classSolution {
public String smallestBeautifulString(String s, int k) {
int n = s.length();
char[] arr = s.toCharArray();
for (int i = n-1; i >= 0; i--) {
for (char c = (char)(arr[i]+1); c < (char)('a'+k); c++) {
if ((i > 0 && c == arr[i-1]) || (i > 1 && c == arr[i-2])) continue;
StringBuilder t =new StringBuilder(s.substring(0, i)).append(c);
for (int j = i+1; j < n; j++) {
for (char d ='a'; d < (char)('a'+k); d++) {
if ((j > 0 && d == t.charAt(j-1)) || (j > 1 && d == t.charAt(j-2))) continue;
t.append(d);
break;
}
}
return t.toString();
}
}
return"";
}
}
classSolution {
funsmallestBeautifulString(s: String, k: Int): String {
val n = s.length
val arr = s.toCharArray()
for (i in n-1 downTo 0) {
for (c in (arr[i]+1)..<('a'+k)) {
if ((i > 0&& c == arr[i-1]) || (i > 1&& c == arr[i-2])) continueval t = StringBuilder(s.substring(0, i)).append(c)
for (j in i+1 until n) {
for (d in'a'..<'a'+k) {
if ((j > 0&& d == t[j-1]) || (j > 1&& d == t[j-2])) continue t.append(d)
break }
}
return t.toString()
}
}
return"" }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution:
defsmallestBeautifulString(self, s: str, k: int) -> str:
n = len(s)
arr = list(s)
for i in range(n-1, -1, -1):
for c in range(ord(arr[i])+1, ord('a')+k):
if (i >0and c == ord(arr[i-1])) or (i >1and c == ord(arr[i-2])):
continue t = arr[:i] + [chr(c)]
for j in range(i+1, n):
for d in range(ord('a'), ord('a')+k):
if (j >0and d == ord(t[j-1])) or (j >1and d == ord(t[j-2])):
continue t.append(chr(d))
breakreturn''.join(t)
return''
impl Solution {
pubfnsmallest_beautiful_string(s: String, k: i32) -> String {
let n = s.len();
letmut arr: Vec<u8>= s.bytes().collect();
for i in (0..n).rev() {
for c in arr[i]+1..b'a'+k asu8 {
if (i >0&& c == arr[i-1]) || (i >1&& c == arr[i-2]) { continue; }
letmut t = arr[..i].to_vec();
t.push(c);
for j in i+1..n {
for d inb'a'..b'a'+k asu8 {
if (j >0&& d == t[j-1]) || (j >1&& d == t[j-2]) { continue; }
t.push(d);
break;
}
}
return String::from_utf8(t).unwrap();
}
}
String::new()
}
}