#K43322. Reverse Words Preserving Spaces
Reverse Words Preserving Spaces
Reverse Words Preserving Spaces
Given a string s, your task is to reverse the order of the words while preserving the exact positions of all spaces. In other words, the spaces in the input string must remain in their original positions, while the words (i.e. sequences of non-space characters) are reversed.
For example, if s = " lead and ", then the output should be " and lead " because the positions of the spaces are kept, but the order of the words "lead" and "and" is reversed.
The input size can be as large as \(10^5\) characters, so please design an efficient solution.
inputFormat
The input consists of a single line containing the string s.
Note that the string may include leading, trailing, or multiple consecutive spaces.
outputFormat
Output a single line containing the modified string with the words in reverse order while preserving the original arrangement of spaces.
## samplehello
hello
</p>