Skip to content

Commit

Permalink
Time: 2 ms (89.01%), Space: 7.2 MB (79.02%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
kamleshjoshi8102 committed Jun 23, 2023
1 parent ae2e2b8 commit f7a7743
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions 0151-reverse-words-in-a-string/0151-reverse-words-in-a-string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Solution {
public:
string reverseWords(string s) {

string res="", temp="";

for(int i=s.size()-1;i>=0;i--)
{
if(s[i]==' ')
{
if(temp.size()>0)
{
reverse(temp.begin(),temp.end());
res+=temp;
res+=" ";
temp="";
}
}
else
{
temp+=s[i];

}
}
if(temp.size()>0)
{
reverse(temp.begin(),temp.end());
res+=temp;
}

while(res[res.size()-1]==' ')
{
res.pop_back();
}


return res;

}
};

0 comments on commit f7a7743

Please sign in to comment.