Reverse Words in a String II

MediumArrayStringSorting

Description

Given a character array s representing a sentence, reverse the order of words in-place. Words are separated by single spaces. Return the result as an array of strings.

Examples

Input:s = ["t","h","e"," ","s","k","y"," ","i","s"," ","b","l","u","e"]
Output:["b","l","u","e"," ","i","s"," ","s","k","y"," ","t","h","e"]
Explanation:

Reverse entire array, then reverse each word. 'the sky is blue' → 'eulb si yks eht' → 'blue is sky the'.

Input:hello world
Output:world hello
Explanation:

With only two words, reversing their order: 'hello world' becomes 'world hello'.

Input:a
Output:a
Explanation:

Single character with no spaces remains unchanged since there's only one word to reverse.

Constraints

  • 1 ≤ s.length ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!