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.
def solve():
s = input()
k = int(input())
n = len(s)
def is_beautiful(st):
for i in range(len(st) - 1):
if st[i] == st[i+1]:
return False
return True
def find_next_beautiful(current_string, index, k):
if index == -1:
return ""
current_char = current_string[index]
for i in range(ord(current_char) + 1, ord('a') + k):
new_char = chr(i)
temp_string = list(current_string)
temp_string[index] = new_char
temp_string = "".join(temp_string)
prefix = temp_string[:index+1]
if len(prefix) >= 2 and prefix[-1] == prefix[-2]:
continue
suffix = ''
possible = True
for j in range(index + 1, n):
found = False
for char_code in range(ord('a'), ord('a') + k):
potential_char = chr(char_code)
if j >= 1 and potential_char == temp_string[j-1]:
continue
if j >= 2 and potential_char == temp_string[j-2]:
continue
suffix += potential_char
temp_string_list = list(temp_string)
temp_string_list[j] = potential_char
temp_string = "".join(temp_string_list)
found = True
break
if not found:
possible = False
break
if possible:
return temp_string
return find_next_beautiful(current_string, index - 1, k)
result = find_next_beautiful(s, n - 1, k)
print(result)
# Example usage (comment out when submitting to LeetCode runner)
# s = "abcz"
# k = 26
# print(find_next_beautiful(s, len(s) - 1, k))
# s = "dc"
# k = 4
# print(find_next_beautiful(s, len(s) - 1, k))
# s = "abaa"
# k = 3
# print(find_next_beautiful(s, len(s) - 1, k))
solve()
The solve
function takes the beautiful string s
and the integer k
as input and returns the lexicographically smallest beautiful string larger than s
. The core logic is in the find_next_beautiful
function, which recursively tries to find a larger beautiful string.
is_beautiful(st)
function:
st
is beautiful. A string is considered beautiful if it doesn't contain any palindrome substring of length 2 or more.find_next_beautiful(current_string, index, k)
function:
current_string
. It starts from the rightmost character of the string (index n-1
).index
becomes -1, it means we have exhausted all possibilities, and no such larger beautiful string exists, so it returns an empty string.current_string[index]
, up to the k
-th character of the alphabet.new_char
, it replaces the character at the current index in current_string
with new_char
. It then checks if the prefix of the modified string is still beautiful.new_char
at the given index doesn't lead to a valid solution. It continues with the next possible new_char
.new_char
is found at the current index, it recursively calls find_next_beautiful
for the previous index.The run-time complexity of the given algorithm is difficult to precisely determine due to the recursive nature and the constraints related to constructing a valid beautiful string. However, we can analyze it in terms of the input size n
(length of the string) and k
(the number of possible characters). Here is a breakdown:
find_next_beautiful
function recursively explores possible characters at each index of the string s
.k
characters.suffix
The space complexity of the algorithm can be analyzed as follows:
temp_string
is used to construct the new string. The size of temp_string
is O(n).