Optimal

class Solution {
public:
    string reverseWords(string s) {
        vector<string> arr;
        stringstream ss(s);
        string word;
        while (ss >> word) {
            arr.push_back(word);
        }
        reverse(arr.begin(), arr.end());
        string ans;
        for (int i = 0; i < arr.size(); i++) {
            ans += (arr[i]);
            if (i != arr.size() - 1) {
                ans += " ";
            }
        }
        return ans;
    }
};