Code Logo

Reverse Words

Published at05 Jan 2026
Pattern Matching Medium 1 views
Like9

In Reverse Words, you are given an input string s, and your goal is to 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, and no leading/trailing spaces.

For example, s = "the sky is blue" becomes "blue is sky the". If s = " hello world ", the answer is "world hello".

Example Input & Output

Example 1
Input
s = "the sky is blue"
Output
"blue is sky the"
Explanation

The words are reversed.

Example 2
Input
s = " hello world "
Output
"world hello"
Explanation

Leading and trailing spaces are removed.

Algorithm Flow

Recommendation Algorithm Flow for Reverse Words
Recommendation Algorithm Flow for Reverse Words

Solution Approach

The simplest way to solve this problem is to reverse the string from end to start and return the reversed result. Since the task is only about flipping character order, you do not need sorting, counting, or any complex data structure. You just need a reliable way to visit the characters in reverse order.

One very common approach is to split the string into characters, reverse that array, and join it back into a string. In JavaScript, that looks like this:

return s.split('').reverse().join('');

This works well for the kind of test cases used here because the inputs are ordinary ASCII-style strings. The call to split('') turns the string into an array of single-character strings. Then reverse() flips their order in place, and join('') stitches them back together without any separator.

Another way to think about the same problem is with two pointers or a backward loop. You could start from the last index, append each character into a result string, and stop when you reach the beginning. That version is also easy to understand and makes the reversal process very explicit. For example:

let result = '';
for (let i = s.length - 1; i >= 0; i--) {
  result += s[i];
}
return result;

Both approaches preserve every character exactly as it appears in the input. That matters because punctuation and spaces are not special cases here. In the long sample with commas and a colon, those characters move into reversed positions just like letters do. The same idea also explains why "Hannah" becomes "hannaH": the string is mirrored exactly, not normalized in any way.

Edge cases are simple too. An empty string returns an empty string because there are no characters to reverse. A one-character string returns itself. Palindromes are not treated specially; they just happen to look similar after reversal if their characters mirror naturally.

The time complexity is O(n) because every character is processed once. The extra space is also O(n) because you build a new reversed result. So the full strategy is: take the input string, reverse the order of all characters, and return the reversed string exactly as a new string.

Best Answers

java
import java.util.*;
class Solution {
    public String[] organize_names(String[] names) {
        String[] res = names.clone();
        Arrays.sort(res);
        return res;
    }
}