Let's explore some string manipulation algorithms. Suppose you are given a string s
and you need to reverse the order of words in the string. Each word is separated by a single space, and there are no leading or trailing spaces. For example, if the input string s
is "the sky is blue", the expected output is "blue is sky the".
s
as input and returns a new string with the words in reversed order.As a follow-up, consider a more complex scenario where the input string s
might contain leading or trailing spaces, or multiple spaces between words. How would you modify your algorithm to handle these cases while still producing the correct reversed word order? For instance, given the input " hello world ", the output should be "world hello". Describe the changes needed and analyze the updated time and space complexity.
This response addresses the interview question regarding string manipulation, specifically reversing the order of words in a given string. It includes a function to reverse the words, explains the approach with time and space complexity analysis, provides test cases, and discusses handling edge cases like leading/trailing spaces and multiple spaces between words.
def reverse_words(s):
words = s.split()
reversed_words = words[::-1]
return " ".join(reversed_words)
The approach involves the following steps:
s
is split into a list of words using the split()
method. By default, split()
splits the string by spaces.[::-1]
. This creates a new reversed list.join()
method.split()
: O(n), where n is the length of the string.[::-1]
: O(n), as it creates a reversed copy of the list.join()
: O(n), where n is the total length of the final string (sum of the lengths of the words plus the spaces).Therefore, the overall time complexity is O(n).
split()
: O(n), as it creates a list of words that, in the worst case (if the string contains only one word), can have length n. In the average case, the space will be proportional to the number of words, which is still bounded by n.Therefore, the overall space complexity is O(n).
print(reverse_words("the sky is blue")) # Output: blue is sky the
print(reverse_words("hello world")) # Output: world hello
print(reverse_words("a")) # Output: a
print(reverse_words("")) # Output: ""
To handle leading/trailing spaces and multiple spaces between words, we can modify the split()
method and add a preprocessing step. The split()
method, when called without arguments, automatically removes leading and trailing spaces and treats multiple spaces as a single delimiter.
Here's the modified function:
def reverse_words_advanced(s):
words = s.split()
reversed_words = words[::-1]
return " ".join(reversed_words)
This version works correctly because s.split()
handles the extra spaces automatically.
print(reverse_words_advanced(" hello world ")) # Output: world hello
print(reverse_words_advanced("the sky is blue")) # Output: blue is sky the
print(reverse_words_advanced(" a b ")) # Output: b a
print(reverse_words_advanced(" ")) # Output: ""
The time and space complexity remain O(n), where n is the length of the input string. The split()
method still iterates through the string, and the reversing and joining operations are still proportional to the number of words, which is bounded by the length of the string.