<p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>

<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>

<p>Return <em>a string of the words in reverse order concatenated by a single space.</em></p>

<p><b>Note</b> that <code>s</code> may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;the sky is blue&quot;
<strong>Output:</strong> &quot;blue is sky the&quot;
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;  hello world  &quot;
<strong>Output:</strong> &quot;world hello&quot;
<strong>Explanation:</strong> Your reversed string should not contain leading or trailing spaces.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;a good   example&quot;
<strong>Output:</strong> &quot;example good a&quot;
<strong>Explanation:</strong> You need to reduce multiple spaces between two words to a single space in the reversed string.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li>
	<li><code>s</code> contains English letters (upper-case and lower-case), digits, and spaces <code>&#39; &#39;</code>.</li>
	<li>There is <strong>at least one</strong> word in <code>s</code>.</li>
</ul>

<p>&nbsp;</p>
<p><b data-stringify-type="bold">Follow-up:&nbsp;</b>If the string data type is mutable in your language, can&nbsp;you solve it&nbsp;<b data-stringify-type="bold">in-place</b>&nbsp;with&nbsp;<code data-stringify-type="code">O(1)</code>&nbsp;extra space?</p>


class Solution {
public:
    // Function to remove extra spaces
    string removeExtraSpaces(const string& s) {
        string result;
        bool inSpace = false;

        for (char ch : s) {
            if (ch != ' ') {
                // If it's a non-space character, add it to the result
                result += ch;
                inSpace = false;
            } else if (!inSpace) {
                // If it's a space and the previous character was not a space, add one space
                result += ' ';
                inSpace = true;
            }
        }

        // Remove leading and trailing spaces if any
        if (!result.empty() && result[0] == ' ') {
            result.erase(result.begin());
        }
        if (!result.empty() && result[result.size() - 1] == ' ') {
            result.erase(result.end() - 1);
        }

        return result;
    }

    // Function to reverse a substring of a string
    string reverseString(string& s, int i, int n) {
        // Use indices to swap elements in the string directly
        while (i < n) {
            swap(s[i], s[n]);
            i++;
            n--;
        }
        return s;
    }

    // Function to reverse words in a string
    string reverseWords(string s) {
        // Remove extra spaces and trim the string
        s = removeExtraSpaces(s);

        // Reverse the entire string
        reverseString(s, 0, s.size() - 1);

        int count = 0;
        int start = 0;

        // Reverse each word in the string
        for (int i = 0; i <= s.size(); i++) {
            if (i == s.size() || s[i] == ' ') {
                reverseString(s, start, i - 1);
                start = i + 1;
            }
        }

        return s;
    }
};
35840