Problem Statement
Given an array of characters representing words separated by single spaces, reverse the order of words in-place (modify the input array directly without extra space). Do not allocate additional arrays or strings. Each word consists of lowercase English letters and is separated by exactly one space.[1][4]
Approach Overview
Use two pointers with a two-step process: first reverse characters within each individual word, then reverse the entire array. This achieves word order reversal while restoring correct character order in each word.[1]
Input/Output Examples
| Input Array | Output Array |
|-------------|--------------|
| ['t','h','e',' ','s','k','y',' ','i','s',' ','b','l','u','e'] | ['b','l','u','e',' ','i','s',' ','s','k','y',' ','t','h','e'] |
| ['c','a','t',' ','i','s'] | ['i','s',' ','c','a','t'] |
[1]
Constraints
1 <= s.length <= 5 * 10^4s[i] is either ' ' (space) or a lowercase English letter