O(n) solution. traverse word by word


#1

Comment body goes here.

string Solution::solve(string s) {
string ans;
int n=s.size(),i=0;

while(i<n)
{
    if(!isspace(s[i]))
    {
        string temp="";
        while(i<n && (!isspace(s[i])) )
        {
           temp+=s[i];
           i++;
        }
        string t1=" ";
        t1+=temp;
        ans=t1+ans;
    }
    else i++;
}
return ans.substr(1,ans.size()-1); //removing space at 1st index.

}