Reverse Words in a String

Try to solve the Reverse Words in a String problem.

Statement

Given a sentence[object Object], reverse the order of its words[object Object] without affecting the order of letters within the given word.

Constraints:

  • The sentence contains English uppercase and lowercase letters, digits, and spaces.

  • 1≤1 \leq sentence.length ≤104\leq 10^4

  • The order of the letters within a word is not to be reversed.

Note: The input string may contain leading or trailing spaces or multiple spaces between words. The returned string, however, should only have a single space separating each word. Do not include any extra spaces.

Examples

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy