Given an input string s
, reverse the order of the words.
A word is defined as a sequence of non-space characters. The words in s
will be separated by at least one space.
Return a string of the words in reverse order concatenated by a single space.
Note that s
may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
Example 1:
Input: s = "the sky is blue" Output: "blue is sky the"
Example 2:
Input: s = " hello world " Output: "world hello" Explanation: Your reversed string should not contain leading or trailing spaces.
Example 3:
Input: s = "a good example" Output: "example good a" Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
Constraints:
1 <= s.length <= 104
s
contains English letters (upper-case and lower-case), digits, and spaces ' '
.s
.Follow-up: If the string data type is mutable in your language, can you solve it in-place with O(1)
extra space?
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 to reverse words in a string is straightforward: split the string into individual words and then reconstruct the string by adding these words in reverse order. This guarantees we'll find the reversed string.
Here's how the algorithm would work step-by-step:
def reverse_words_brute_force(input_string):
words = input_string.split()
reversed_string = ""
# Handle empty input
if not words:
return reversed_string
# Start with the last word
reversed_string = words[-1]
#Iterate backwards and build the reversed string
for index in range(len(words) - 2, -1, -1):
# Add space before adding another word
reversed_string += " " + words[index]
return reversed_string
The most efficient way to reverse words in a string is to first split the string into individual words, then reverse the order of these words, and finally join them back together. We'll also handle extra spaces to ensure a clean reversed string.
Here's how the algorithm would work step-by-step:
def reverse_words_in_a_string(input_string):
words = input_string.split()
# Filter out empty strings resulting from multiple spaces
words = [word for word in words if word]
# Reverse the order of the words.
reversed_words = words[::-1]
# Join the reversed words with a single space.
reversed_string = ' '.join(reversed_words)
# Remove any leading or trailing spaces.
return reversed_string.strip()
Case | How to Handle |
---|---|
Null or empty input string | Return an empty string immediately to avoid null pointer exceptions or unnecessary processing. |
String with only spaces | After trimming, the string will be empty, resulting in an empty output string. |
String with leading and trailing spaces | Trim the string to remove leading and trailing spaces before reversing the words. |
String with multiple spaces between words | Reduce multiple spaces to single spaces during the word extraction process. |
String with a single word | Return the single word after trimming any leading/trailing spaces. |
Very long string | Ensure the algorithm handles large strings efficiently without excessive memory allocation by using StringBuilder or similar optimized mechanisms. |
String containing non-ASCII characters | Ensure the splitting and joining logic correctly handles Unicode characters. |
String contains only a single space character | The trimming process will result in an empty string, resulting in an empty string output. |