dsa · easy

Reverse Words in a String

GreyOrangeStringTwo PointersFoundation

Reverse the order of words in s. Collapse internal whitespace; trim ends.

Arguments

**Example:** "the sky is blue""blue is sky the".

Naukri GreyOrange pack.

Constraints

1 <= s.length <= 4*10^4 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.

Examples

Example 1

Input:
"the sky is blue"

Expected:
blue is sky the

Example 2

Input:
"  hello world  "

Expected:
world hello

Open in the Dojo editor