Given a string s, reverse the order of its words. A word is a maximal run of non-space characters. The result must contain the words in reverse order separated by exactly one space: leading and trailing spaces are removed, and runs of several spaces between words collapse to a single space.
Given a string s, reverse the order of its words. A word is a maximal run of non-space characters. The result must contain the words in reverse order separated by exactly one space: leading and trailing spaces are removed, and runs of several spaces between words collapse to a single space.
Example 1:
Input: {"s":"the sky is blue"}
Output: blue is sky the
Constraints:
1≤s.length≤104
s contains English letters (upper-case and lower-case), digits, and spaces
There is at least one word in s
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"s":"the sky is blue"}
Read s from the right end, so the last word is found first. Start i at index 14; words collects them in the order they are met.