Search⌘ K

Reverse Words in a Sentence

Explore how to reverse the order of words in a sentence without changing the order of letters within each word. This lesson teaches an in-place method that involves reversing the entire string and then each individual word, achieving linear time and constant space complexity.

Statement

Given a sentence (an array of characters), reverse the order of its words without affecting the order of letters within a given word. All operations must be done in place.

Example

The “Hello World” string should be reversed as “World Hello”.

g a Hello World b World Hello a->b

Here’s another example of reversing words in a sentence:

g a The quick brown fox jumped over the lazy dog. b dog. lazy the over jumped fox brown quick The a->b

Sample input

"Hello world."

Expected output

"world. Hello"

Note: The ...