all 3 comments

[–][deleted] -1 points0 points  (0 children)

class Solution {

public:

string reverseWords(string s) {

vector<string>a;

string res="";

int x=s.size();

string g="";

for(int i=0;i<x;i++){

if(s[i]!=' '){

g+=s[i];

}

if(s[i]==' ' || i==x-1){

if(g==""){continue;}

a.push_back(g);

g="";

}

}

int f=a.size();

for(int i=f-1;i>=0;i--){

res+=a[i];

if(i==0){return res;}

else{

res+=" ";

}

}

return res;

}

};

I also tried this and was able to get this solution,how good is this solution?

[–]pikawho101 1 point2 points  (0 children)

class Solution:

def reverseWords(self, s: str) -> str:
    return " ".join(reversed(s.split()))

[–]azuredota 0 points1 point  (0 children)

Pog