Reversing the words in a string while handling multiple spaces efficiently is a common problem in string manipulation. Let's tackle LeetCode 151: Reverse Words in a String with an optimized solution and without using predefined methods like split or trim.
Given a string s, reverse the order of the words in the string:
A word is defined as a sequence of non-space characters.
Return the reversed string with a single space between the words. Extra spaces before, after, or between words should be removed.