We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1f231b5 commit 0251614Copy full SHA for 0251614
Algorithms/Strings/ReverseWordsInaString.cpp
@@ -0,0 +1,26 @@
1
+//https://leetcode.com/problems/reverse-words-in-a-string/
2
+
3
+class Solution {
4
+public:
5
+ string reverseWords(string s) {
6
+ s+=" ";
7
+ string temp = "";
8
+ vector<string>v;
9
10
+ for(char ch: s){
11
+ if(ch!=' '){
12
+ temp+=ch;
13
+ }
14
+ else {
15
+ if(temp.length())
16
+ v.push_back(temp);
17
+ temp = "";
18
19
20
+ string result = "";
21
+ if(v.size() && v[v.size()-1].length()) result+=v[v.size()-1];
22
+ for(int i = v.size()-2; i >=0; i--)
23
+ result+=" "+v[i];
24
+ return result;
25
26
+};
0 commit comments