2663. Lexicographically Smallest Beautiful String

Difficulty:
Related Topics:
Similar Questions:

Problem

A string is beautiful if:

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.

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:

Solution (Java)

class Solution {
    public String smallestBeautifulString(String s, int k) {
        char[] ch = s.toCharArray();
        int i = ch.length - 1;
        while (i >= 0) {
            ch[i]++;
            if (ch[i] - 'a' == k) {
                i--;
            } else if ((i - 1 < 0 || ch[i - 1] != ch[i]) && (i - 2 < 0 || ch[i - 2] != ch[i])) {
                break;
            }
        }
        if (i < 0) return "";
        for (int j = i + 1; j < ch.length; j++) {
            SortedSet<Character> set = new TreeSet<>(Arrays.asList('a', 'b', 'c'));
            if (j - 2 >= 0) set.remove(ch[j - 2]);
            if (j - 1 >= 0) set.remove(ch[j - 1]);
            ch[j] = set.first();
        }
        return new String(ch);
    }
}

Explain:

nope.

Complexity: