dsa · easy
Reverse Words in a String
GreyOrangeStringTwo PointersFoundation
Reverse the order of words in s. Collapse internal whitespace; trim ends.
Arguments
s— the input string (or list of characters, if reversing in place)
**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