<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:
    /*
    std::vector<std::string> splitString(const std::string& str, char delimiter) {
        std::vector<std::string> tokens;
        std::stringstream ss(str);
        std::string token;

        while (std::getline(ss, token, delimiter)) {
            if (!token.empty()) {
                tokens.push_back(token);
            }
        }

        return tokens;
    }
    std::string joinStrings(const std::vector<std::string>& elements, const std::string& delimiter) {
        std::ostringstream os;
            for (auto it = elements.begin(); it != elements.end(); ++it) {
                if (it != elements.begin()) {
                    os << delimiter;
                }
                os << *it;
            }
            return os.str();
    }
    */
    vector<string> splitString2(string str, char delimiter) {
        vector<string> tokens;
        string token;
        stringstream ss(str);
        while (getline(ss,token,delimiter)) {
            if (!token.empty()) {
                tokens.push_back(token);
            }
        }
        return tokens;       
    }
    string joinStrings2(vector<string> elements, string delimiter) {
        string os;
        for (auto it=elements.begin();it!=elements.end(); ++it) {
            if (it!=elements.begin()) {
                os.append(delimiter);
            }
            os.append(*it);
        }
        return os;
    }
    
    string reverseWords(string s) {
        std::vector<std::string> s_split = splitString2(s, ' ');
        for (string i : s_split) {
          cout << i << "\n";
        }
        std::vector<std::string> ans;
        
        for (int i = s_split.size() - 1; i >= 0; --i) {
            if (!s_split[i].empty()) {
                ans.push_back(s_split[i]);
            }
        }
        
        return joinStrings2(ans, " ");    
    }
};
12440