Reverse Words In String Time : O(n) Space : O(n) 12345678910111213141516171819202122232425using namespace std;string parse(string& s, int& p, bool space) { string res = ""; while(p < s.length() and (s[p] == ' ') == space) { res.push_back(s[p++]); } return res;}string reverseWordsInString(string str) { if(str.length() == 0) return ""; vector<string> tokens; int p = 0; bool space = str[0] == ' '; while(p < str.length()) { tokens.push_back(parse(str, p, space)); tokens.push_back(parse(str, p, !space)); } reverse(begin(tokens), end(tokens)); string res = ""; for(auto& t : tokens) { res += t; } return res;}