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 <= 10^5
4 <= k <= 26
s
is a beautiful string.def find_beautiful_string(s: str, k: int) -> str:
n = len(s)
chars = [chr(ord('a') + i) for i in range(k)]
def is_beautiful(test_str: str) -> bool:
for i in range(len(test_str) - 1):
if test_str[i] == test_str[i+1]:
return False
return True
def increment_string(index: int) -> str:
if index < 0:
return ""
for char in chars:
temp_s = list(s)
temp_s[index] = char
temp_str = "".join(temp_s)
if is_beautiful(temp_str):
if temp_str > s:
return temp_str
s_list = list(s)
s_list[index] = 'a'
s = "".join(s_list)
return increment_string(index-1)
result = increment_string(n-1)
if not result:
return ""
# Ensure that the incremented string consists of only the first k letters
if not all(c in chars for c in result):
return ""
# Ensure that the incremented string is beautiful
if not is_beautiful(result):
return ""
return result
# Example usage:
s = "abcz"
k = 26
print(find_beautiful_string(s, k))
s = "dc"
k = 4
print(find_beautiful_string(s, k))
s = "aba"
k = 3
print(find_beautiful_string(s, k))
s = "abca"
k = 3
print(find_beautiful_string(s, k))
find_beautiful_string(s, k)
that takes a beautiful string s
and an integer k
as input.chars
containing the first k
lowercase English letters.is_beautiful
Helper Function: A helper function is_beautiful(test_str)
checks if a given string test_str
is beautiful according to the problem definition (i.e., it consists of the first k
letters and contains no palindrome substring of length 2 or more).increment_string
Helper Function:
increment_string(index)
. This function attempts to increment the character at the given index
to find the next lexicographically larger beautiful string.chars
) and tries replacing the character at the current index
with each character from chars
.temp_str
) is beautiful and lexicographically larger than the original string s
, the function returns it.index
, it resets the character at that index to 'a' and recursively calls increment_string
for the previous index (index - 1
). This is essentially handling the carry-over when incrementing a number.increment_string(n-1)
to start incrementing from the last character of the input string s
.result
is empty, and returns an empty string if that's the case, signifying that no solution was found.is_beautiful(result)
.s
, or an empty string if no such string exists.Time Complexity:
The worst-case time complexity is O(n * k), where n is the length of the string and k is the number of allowed characters. In the worst case, the increment_string
function might have to explore all possible characters at each position of the string. This occurs when the initial characters need to be rolled over multiple times. Specifically, the increment_string
function has a loop that iterates k
times, and the depth of the recursion can be at most n
. So, it's O(n * k).
Space Complexity:
The space complexity is primarily determined by the recursion depth of the increment_string
function, which can be at most O(n) in the worst case. Additionally, temporary strings temp_str
and s_list
are created within the increment_string
function, but their space is also bounded by O(n). Therefore, the overall space complexity is O(n).