You are given a string text of words that are placed among some number of spaces. Each word consists of one or more lowercase English letters and are separated by at least one space. It's guaranteed that text contains at least one word.
Rearrange the spaces so that there is an equal number of spaces between every pair of adjacent words and that number is maximized. If you cannot redistribute all the spaces equally, place the extra spaces at the end, meaning the returned string should be the same length as text.
Return the string after rearranging the spaces.
Example 1:
Input: text = " this is a sentence " Output: "this is a sentence" Explanation: There are a total of 9 spaces and 4 words. We can evenly divide the 9 spaces between the words: 9 / (4-1) = 3 spaces.
Example 2:
Input: text = " practice makes perfect" Output: "practice makes perfect " Explanation: There are a total of 7 spaces and 3 words. 7 / (3-1) = 3 spaces plus 1 extra space. We place this extra space at the end of the string.
Constraints:
1 <= text.length <= 100text consists of lowercase English letters and ' '.text contains at least one word.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 rearranging spaces is like trying out every single way you could possibly distribute the spaces between the words. We essentially test every single arrangement and choose the best one. It's thorough, but not very efficient.
Here's how the algorithm would work step-by-step:
def rearrange_spaces_brute_force(text): words = text.split() number_of_words = len(words)
number_of_spaces = text.count(' ')
best_arrangement = ""
for spaces_between_words in range(1, number_of_spaces + 1): # Check if this arrangement is possible. if number_of_words == 1 and spaces_between_words != number_of_spaces: continue
if number_of_words > 1 and (number_of_words - 1) * spaces_between_words > number_of_spaces: continue
extra_spaces = number_of_spaces - (number_of_words - 1) * spaces_between_words if number_of_words > 1 else number_of_spaces if number_of_words == 1:
arrangement = words[0] + ' ' * extra_spaces
else:
arrangement = (' ' * spaces_between_words).join(words) + ' ' * extra_spaces
# This is the only valid arrangement so far. best_arrangement = arrangement return best_arrangementThe best way to rearrange spaces is to figure out how many gaps you need to fill and how many spaces you have. Then, evenly distribute the spaces among the gaps. Finally, handle the case where the spaces don't divide evenly or if there's only one word.
Here's how the algorithm would work step-by-step:
def rearrange_spaces(text: str) -> str:
words = text.split()
number_of_words = len(words)
number_of_spaces = text.count(' ')
if number_of_words == 1:
return words[0] + ' ' * number_of_spaces
# Calculate spaces between words.
spaces_between = number_of_spaces // (number_of_words - 1)
# Determine the remaining spaces after even distribution.
remaining_spaces = number_of_spaces % (number_of_words - 1)
result = ''
for i in range(number_of_words - 1):
result += words[i] + ' ' * spaces_between
result += words[-1] + ' ' * remaining_spaces
return result| Case | How to Handle |
|---|---|
| Empty input string | Return an empty string immediately since there are no words to rearrange. |
| Input string with only spaces | Return the original string if it contains only spaces since there are no words to rearrange. |
| Input string with a single word | Append the remaining spaces after the single word. |
| Input string with leading and trailing spaces | Trim the leading and trailing spaces before processing the string to avoid incorrect word and space counts. |
| Input string with multiple spaces between words | Normalize spaces to a single space between words during word extraction to ensure accurate word count and space distribution. |
| Zero spaces in the input | If no spaces exist in the input (e.g., a single word), return the word itself. |
| Large input string that may cause memory issues. | Use a StringBuilder instead of string concatenation for better memory efficiency. |
| Input has a very large number of words with a very small number of spaces. | Ensure that the integer division for space distribution doesn't result in incorrect calculation due to overflow or precision issues by using appropriate data types. |