Reverse Words in a String II

Medium

Description

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

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 words.

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!