Using greedy without proof
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
A string is beautiful if:
k letters of the English lowercase alphabet.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.
"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 <= 1054 <= k <= 26s is a beautiful string.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Greedy
"abcz" 26
"dc" 4
smallest-string-with-swaps)find-palindrome-with-fixed-length)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2663: Lexicographically Smallest Beautiful String
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 "";
}
}
// Accepted solution for LeetCode #2663: Lexicographically Smallest Beautiful String
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 ""
}
# Accepted solution for LeetCode #2663: Lexicographically Smallest Beautiful String
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 ''
// Accepted solution for LeetCode #2663: Lexicographically Smallest Beautiful String
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2663: Lexicographically Smallest Beautiful String
// 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 "";
// }
// }
// Accepted solution for LeetCode #2663: Lexicographically Smallest Beautiful String
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 '';
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.