Reverse Sentence Words

String Manipulation Easy Daily

Problem Description

Write a function that takes in a sentence (a string of words separated by spaces) and returns a new string with the words in reversed order. The characters within each word should remain in the same order.

For example:

  • Input: "Hello world from May 15"
  • Output: "15 May from world Hello"

Note:

  • The input string may contain extra spaces. You may assume that words are separated by one or more spaces.

In celebration of International Families Day on May 15, imagine reversing the order of family members' names in an invitation card for a fun twist!

Function Signature (pseudo-code):

function reverseWords(sentence: string) -> string

Your solution should run in O(n) time complexity, where n is the length of the sentence.