186. Reverse Words in a String II
Problem
Reverse the order of words in a character array in-place. Words are separated by single spaces.
Given a character array s (a string represented as an array of characters), reverse the order of words in-place. A word is defined as a maximal sequence of characters without spaces. There are no leading or trailing spaces, and exactly one space separates words.
This must be done in O(1) extra space (in-place modification).
Examples
Input: ["t","h","e"," ","s","k","y"," ","i","s"," ","b","l","u","e"]
Output: ["b","l","u","e"," ","i","s"," ","s","k","y"," ","t","h","e"]
Input: ["a"," ","b"]
Output: ["b"," ","a"]
Hints
First reverse the entire array.
Then reverse each word individually.
This achieves O(1) space complexity.
Related Problems
186. Reverse Words in a String II
Reverse the order of words in a character array in-place. Words are separated by single spaces.