Reverse Words in a String Efficient String Manipulation Technique

Problem Statement

Given a string s that contains words separated by spaces, reverse the order of words in the string.

Remove any leading or trailing spaces and reduce multiple spaces between words to a single space in the output.

Note: Do not reverse the characters of individual words—only the word order should be reversed.

Examples

Input Output Description
"the sky is blue" "blue is sky the" Words are reversed in order
" hello world " "world hello" Extra spaces removed and words reversed
"a good example" "example good a" Multiple spaces between words collapsed
" Bob Loves Alice " "Alice Loves Bob" Trimmed and reversed correctly

Visualization Player

Solution

To reverse words in a string:

{ "array": [" the sky is blue "], "showIndices": false, "highlightIndicesGreen": [] }
  1. First, trim the string to remove leading/trailing spaces.
    { "array": ["the sky is blue"], "showIndices": false, "highlightIndicesGreen": [] }
  2. Then split the string into individual words using space as the delimiter.
    { "array": ["the"], "showIndices": false, "emptyCompIndices": [1,3] }
    { "array": ["sky"], "showIndices": false }
    { "array": [""], "showIndices": false }
    { "array": ["is"], "showIndices": false }
    { "array": ["blue"], "showIndices": false }
  3. Ignore empty strings caused by multiple spaces.
    { "array": ["the"], "showIndices": false, "emptyCompIndices": [1,3] }
    { "array": ["sky"], "showIndices": false }
    { "array": ["is"], "showIndices": false }
    { "array": ["blue"], "showIndices": false }
  4. Reverse the list of words,
    { "array": ["blue"], "showIndices": false }
    { "array": ["is"], "showIndices": false }
    { "array": ["sky"], "showIndices": false }
    { "array": ["the"], "showIndices": false, "emptyCompIndices": [1,3] }
  5. And join them with a single space.
    { "array": ["blue is sky the"], "showIndices": false, "highlightIndicesGreen": [] }

This method ensures the output has no extra spaces and all words are in reversed order.

Algorithm Steps

  1. Trim leading and trailing spaces from the string s.
  2. Split the string into a list of words using whitespace as the delimiter.
  3. Filter out any empty strings caused by multiple spaces.
  4. Reverse the list of words.
  5. Join the words with a single space to form the final output.

Code

Python
Java
JavaScript
C
C++
C#
Go
def reverse_words(s):
    # Trim and split the string
    words = s.strip().split()
    # Reverse the list of words
    words.reverse()
    # Join with a single space
    return ' '.join(words)

# Sample Input
s = "  hello   world  "
print(reverse_words(s))

Time Complexity

CaseTime ComplexityExplanation
Best CaseO(n)Each character in the string is processed once during trimming, splitting, and joining.
Average CaseO(n)All operations—trim, split, reverse, and join—are linear with respect to the input size.
Worst CaseO(n)Even in the worst case with many extra spaces, we still process each character only once.

Space Complexity

O(n)

Explanation: We use extra space to store the list of words and the final result string.