<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:
    // My approach - using three pointer i, j and k
    // Time Complexity: O(n) to traverse string s
    // + O(2m) => O(m) to traverse string str and reverse each word
    // + O(m) to reverse string str
    // note: m is the length of string str - str is formed by removing all extra spaces from string s
    // Space Complexity: O(n) extra space, extra string str is used
    string reverseWords(string s) {
        int n = s.length();
        string str = "";

        // 1. moving string s to new string by removing all unrequired trailing spaces
        for(int i=0; i<n; i++){
            if(s[i] != ' ' || (i > 0 && s[i] == ' ' && s[i-1] != ' ')){
                str.push_back(s[i]);
            }
        }

        // if last index of str has a space left, then remove it
        if(str.back() == ' '){
            str.erase(str.length()-1, 1);
        }

        // using i and j to swap each word
        // using k to traverse the complete string
        int m = str.length();
        int i = 0, j = 0, k = 0;

        // 2. swapping each word of the string str
        while(k < m){
            while(k != m && str[k] != ' '){
                j++; k++;
            }

            j--;
            k++;

            // now i points to 1st index of the current word
            // and j points to the last index of the current word
            // now swap the word
            while(i < j){
                swap(str[i], str[j]);
                i++; j--;
            }

            i = k;
            j = k;
        }

        // 3. reversing the complete string to get the output string
        i = 0; j = m-1;
        while(i < j){
            swap(str[i], str[j]);
            i++; j--;
        }

        return str;
    }
};
8800