You are given a 0-indexed string s
and a 0-indexed integer array spaces
that describes the indices in the original string where spaces will be added. Each space should be inserted before the character at the given index.
s = "EnjoyYourCoffee"
and spaces = [5, 9]
, we place spaces before 'Y'
and 'C'
, which are at indices 5
and 9
respectively. Thus, we obtain "Enjoy Your Coffee"
.Return the modified string after the spaces have been added.
Example 1:
Input: s = "LeetcodeHelpsMeLearn", spaces = [8,13,15] Output: "Leetcode Helps Me Learn" Explanation: The indices 8, 13, and 15 correspond to the underlined characters in "LeetcodeHelpsMeLearn". We then place spaces before those characters.
Example 2:
Input: s = "icodeinpython", spaces = [1,5,7,9] Output: "i code in py thon" Explanation: The indices 1, 5, 7, and 9 correspond to the underlined characters in "icodeinpython". We then place spaces before those characters.
Example 3:
Input: s = "spacing", spaces = [0,1,2,3,4,5,6] Output: " s p a c i n g" Explanation: We are also able to place spaces before the first character of the string.
Constraints:
1 <= s.length <= 3 * 105
s
consists only of lowercase and uppercase English letters.1 <= spaces.length <= 3 * 105
0 <= spaces[i] <= s.length - 1
spaces
are strictly increasing.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 involves exploring every single possible way to insert spaces into the string at the specified positions. We generate all combinations of placing spaces and then choose the best one. Think of it as trying out every possibility, no matter how inefficient it might seem at first.
Here's how the algorithm would work step-by-step:
def add_spaces_brute_force(input_string, space_indices):
possible_strings = []
number_of_indices = len(space_indices)
# Generate all possible combinations of spaces
for i in range(2**number_of_indices):
current_string = list(input_string)
spaces_to_add = []
# Determine which indices should have spaces based on the binary representation of i
for index_position in range(number_of_indices):
if (i >> index_position) & 1:
spaces_to_add.append(space_indices[index_position])
# Sort the indices in reverse order to avoid shifting issues when inserting
spaces_to_add.sort(reverse=True)
# Insert the spaces into the string
for index in spaces_to_add:
current_string.insert(index, ' ')
possible_strings.append("".join(current_string))
# This part would normally involve criteria to select the best string,
# but since it's not specified, we return the first one
if possible_strings:
return possible_strings[0]
#Handle the case when the input string is empty
return input_string
The efficient solution focuses on building the final string piece by piece. It figures out how many words fit on each line and then spaces them out accordingly, rather than trying all possible combinations.
Here's how the algorithm would work step-by-step:
def add_spaces_to_string(original_string, spaces_to_add):
string_with_spaces = ""
space_index = 0
for i, character in enumerate(original_string):
# Add space if the current index is in spaces_to_add
if space_index < len(spaces_to_add) and i == spaces_to_add[space_index]:
string_with_spaces += " "
space_index += 1
string_with_spaces += character
return string_with_spaces
Case | How to Handle |
---|---|
Null or empty input string | Return an empty string or throw an IllegalArgumentException, depending on the desired behavior defined in the requirements. |
Null or empty array of space insertion indices | Return the original string unchanged, since no spaces need to be added. |
Empty string and empty array of indices | Return an empty string, as there is nothing to process. |
Space insertion indices out of bounds (negative or greater than string length) | Filter out invalid indices or throw an exception to indicate invalid input. |
Duplicate space insertion indices | The algorithm should only insert a space once at the specific location even if the index appears multiple times; use a set to deduplicate the indices. |
Space insertion indices in reverse order | Sort the indices in ascending order before iterating to ensure correct insertion. |
Very long input string and large number of space insertion indices leading to possible memory issues. | Use a StringBuilder to efficiently construct the modified string and avoid excessive memory allocation. |
Adjacent or overlapping space insertion indices (e.g., inserting spaces at indices 1 and 2). | After inserting a space, increment the subsequent indices to account for the increased string length caused by the space insertion. |