 All Problems
Reverse Words in a String
easy
strings
two pointers
microsoft
amazon
apple
flipkart

Given an input string s, reverse the order of the words.

A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.

Return a string of the words in reverse order concatenated by a single space. Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words with no leading or trailing spaces.

Example 1:

Input: the sky is blue
Output: blue is sky the

Example 2:

Input:   hello world
Output: world hello

Example 3:

Input: a good   example
Output: example good a

Constraints:

  • 1 ≤ s.length ≤ 10⁴
  • s contains English letters, digits, and spaces

Input format: A single line string (may have leading/trailing/extra spaces).

Output format: Words in reversed order, separated by single spaces.

Run to check your code against the sample cases, or submit to run every case