Strings are immutable in Python: you cannot change a character in place, so string problems are usually solved by building a new string from pieces. The split and join methods are the workhorses.
Given a string s made of words separated by spaces, return a string with the words in reverse order. Words are separated by a single space in the output, and there must be no leading or trailing spaces, even if the input has extra spaces.
Examples
Input: s = "the sky is blue"
Output: "blue is sky the"
Input: s = " hello world "
Output: "world hello"
Explanation: leading and trailing spaces are dropped.
Input: s = "a good example"
Output: "example good a"
Explanation: several spaces between words collapse to one.
Constraints
1 <= len(s) <= 10**4scontains letters, digits and spaces, and at least one word
Goals
- Break a string into words with split() and rebuild it with join()
- Normalise messy whitespace without character-by-character loops