
Reverse Words in a String II (medium)
Problem Statement
Given a character array s, return the updated array after reversing the order of words in the array.
A word is defined as a sequence of non-space characters, and the words are separated by one space character. The input array does not contain any leading or trailing spaces, and the words are always separated by a single space.
Note: Your code must solve the problem without using any extra space.
Examples
-
Example 1:
- Input:
["h","e","l","l","o"," ","w","o","r","l","d"] - Expected Output:
["w","o","r","l","d"," ","h","e","l","l","o"] - Justification: The original string is "hello world". Reversing the words gives "world hello".
- Input:
-
Example 2:
- Input:
["m","o","r","n","i","n","g"," ","s","k","y"," ","i","s"," ","b","r","i","g","h","t"] - Expected Output:
["b","r","i","g","h","t"," ","i","s"," ","s","k","y"," ","m","o","r","n","i","n","g"] - Justification: The original string is "morning sky is bright". Reversing the words gives "bright is sky morning".
- Input:
-
Example 3:
- Input:
["a","l","o","n","e"] - Expected Output:
["a","l","o","n","e"] - Justification: There's only one word "alone", so the reverse is the same as the original.
- Input:
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory