Skip to content

Latest commit

 

History

History

2663

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

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.

Companies: Google

Related Topics:
String, Greedy

Similar Questions:

Solution 1. Backtracking

// OJ: https://leetcode.com/problems/lexicographically-smallest-beautiful-string
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
class Solution {
public:
    string smallestBeautifulString(string s, int k) {
        int N = s.size(), changed = false;
        function<bool(int)> dfs = [&](int i) {
            if (i == N) return true;
            char mn = changed ? 'a' : (s[i] + (i == N - 1)), init = s[i];
            for (char c = mn; c <= 'a' + k - 1; ++c) {
                if ((i - 1 >= 0 && s[i - 1] == c) || (i - 2 >= 0 && s[i - 2] == c)) continue;
                changed |= init != c;
                s[i] = c;
                if (dfs(i + 1)) return true;
            }
            return false;
        };
        return dfs(0) ? s : "";
    }
};

Solution 2. Greedy

// OJ: https://leetcode.com/problems/lexicographically-smallest-beautiful-string
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1) if changing in-place in `s` is allowed; otherwise O(N)
class Solution {
public:
    string smallestBeautifulString(string s, int k) {
        int N = s.size();
        auto valid = [&](int i) {
            return (i - 1 < 0 || s[i] != s[i - 1]) && (i - 2 < 0 || s[i] != s[i - 2]);
        };
        for (int i = N - 1; i >= 0; --i) {
            ++s[i];
            while (!valid(i)) ++s[i];
            if (s[i] < 'a' + k) {
                for (++i; i < N; ++i) {
                    for (s[i] = 'a'; !valid(i); ++s[i]);
                }
                return s;
            }
        };
        return "";
    }
};