less than 1 minute read

Given a list of strings sentence where each sentence[i] is a string with single character, reverse the order of the words in the sentence.

You may assume there’s no extraneous spaces in the sentence. Can you do modify sentence in-place and solve in \(\mathcal{O}(1)\) space?

Constraints

  • n ≤ 10,000 where n is the length of sentence.

https://binarysearch.com/problems/Sentence-Reversal

Examples

Example 1

Input

  • sentence = ['h', 'i', ' ', 'w', 'o', 'r', 'l', 'd']

Output

  • answer = ['w', 'o', 'r', 'l', 'd', ' ', 'h', 'i']

Solution

Leave a comment