You are given a binary string s
and a positive integer k
.
A substring of s
is beautiful if the number of 1
's in it is exactly k
.
Let len
be the length of the shortest beautiful substring.
Return the lexicographically smallest beautiful substring of string s
with length equal to len
. If s
doesn't contain a beautiful substring, 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 = "100011001", k = 3
Output: "11001"
Example 2:
Input: s = "1011", k = 2
Output: "11"
Example 3:
Input: s = "000", k = 1
Output: ""
Constraints:
1 <= s.length <= 100
1 <= k <= s.length
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for this problem involves trying every possible string within the specified length to see if it meets our criteria for being a beautiful string. We then filter these strings to only keep the beautiful ones. Finally, we find the shortest and smallest (lexicographically) one from the beautiful strings we've kept.
Here's how the algorithm would work step-by-step:
def shortest_and_lexicographically_smallest_beautiful_string(initial_string, max_length):
shortest_beautiful_string = None
min_length = len(initial_string)
def is_beautiful(test_string):
for substring_length in range(1, len(test_string) // 2 + 1):
for start_index in range(len(test_string) - 2 * substring_length + 1):
first_substring = test_string[start_index:start_index + substring_length]
second_substring = test_string[start_index + substring_length:start_index + 2 * substring_length]
if first_substring == second_substring:
return False
return True
def generate_strings(current_string, current_length, all_beautiful_strings):
if current_length > max_length:
return
if current_length >= min_length:
if is_beautiful(current_string):
all_beautiful_strings.append(current_string)
for char in ['a', 'b', 'c']:
generate_strings(current_string + char, current_length + 1, all_beautiful_strings)
all_beautiful_strings = []
generate_strings("", 0, all_beautiful_strings)
# After generating all strings, we search for the shortest
if all_beautiful_strings:
shortest_length = min(len(beautiful_string) for beautiful_string in all_beautiful_strings)
shortest_beautiful_strings = [beautiful_string for beautiful_string in all_beautiful_strings if len(beautiful_string) == shortest_length]
# If multiple strings have the same shortest length, find the lexicographically smallest
shortest_beautiful_string = min(shortest_beautiful_strings)
return shortest_beautiful_string
We want to find the shortest string that also comes first alphabetically and satisfies the 'beautiful' condition. To do this, we focus on fixing the fewest characters possible from the original string and ensuring our changes lead to an earlier alphabetical order if possible.
Here's how the algorithm would work step-by-step:
def shortest_and_lexicographically_smallest_beautiful_string(input_string, period):
string_length = len(input_string)
def is_beautiful(current_string, current_period):
for i in range(current_period, len(current_string)):
if current_string[i] == current_string[i - current_period]:
return False
return True
if is_beautiful(input_string, period):
return input_string
for i in range(string_length):
if i >= period and input_string[i] == input_string[i - period]:
# Found the index where the 'beautiful' condition is broken.
for char_code in range(ord('a'), ord('a') + period):
replacement_char = chr(char_code)
temp_string = list(input_string)
temp_string[i] = replacement_char
temp_string = "".join(temp_string)
if i >= period and temp_string[i] == temp_string[i - period]:
continue
# After finding the replacement character,
# build the rest of the string.
for j in range(i + 1, string_length):
for next_char_code in range(ord('a'), ord('a') + period):
next_replacement_char = chr(next_char_code)
temp_string_list = list(temp_string)
temp_string_list[j] = next_replacement_char
potential_string = "".join(temp_string_list)
if j >= period and potential_string[j] == potential_string[j - period]:
continue
else:
temp_string = potential_string
break
#Check if new string is beautiful before returning
if is_beautiful(temp_string, period):
return temp_string
#If no lexicographically smaller string found, reconstruct string
temp_string = list(input_string)
#We need to reconstruct and find the smallest lexicographical
# beautiful string.
for j in range(i, string_length):
for next_char_code in range(ord('a'), ord('a') + period):
next_replacement_char = chr(next_char_code)
temp_string_list = list(input_string)
temp_string_list[j] = next_replacement_char
potential_string = "".join(temp_string_list)
if j >= period and potential_string[j] == potential_string[j - period]:
continue
else:
input_string = potential_string
break
return input_string
return input_string
Case | How to Handle |
---|---|
Input string s is null or empty | Return an empty string or an appropriate error message as no beautiful string can be generated. |
Input k is zero or negative | If k is zero, an empty string is already beautiful so return the original; if k is negative throw an IllegalArgumentException as pattern length cannot be negative. |
Input string s has length less than k | Construct the lexicographically smallest beautiful string of length k using the first k characters of the alphabet. |
No valid 'beautiful' string exists that's lexicographically smaller than input. | Return the smallest beautiful string of length n (same length as the original input string). |
Input string s consists of all the same characters. | Find the first position where we can replace the character with the next character in the alphabet while staying within constraints of k uniqueness. |
k is greater than 26 (number of lowercase English letters) | Return null or throw exception since it's impossible to have a beautiful string with more than 26 unique characters using only lowercase letters. |
Large input string length leading to potential performance issues | Ensure the algorithm has at most O(n) complexity so it will scale efficiently. |
Handling edge cases when you reach the end of the alphabet (z) | Backtrack and increment the previous character when 'z' is reached and constraint violated, and handle carry-over effect if necessary. |