Skip to content

Commit 53e39c5

Browse files
Create 1859. Sorting the Sentence.cpp
1 parent b8813a8 commit 53e39c5

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

1859. Sorting the Sentence.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
string sortSentence(string s) {
4+
5+
int n = s.size();
6+
vector<pair<int,string>>temp;
7+
8+
int j = 0;
9+
string ans = "";
10+
while (j < n){
11+
if( 48 <= s[j] && s[j] <= 57 ){
12+
temp.push_back({s[j]-'0',ans});
13+
ans = "";
14+
j++;
15+
}
16+
17+
if(s[j] == ' ') j++;
18+
ans += s[j];
19+
j++;
20+
}
21+
22+
sort(temp.begin(),temp.end());
23+
24+
string ans1 = "";
25+
for(auto it : temp){
26+
ans1 += it.second;
27+
ans1 += " ";
28+
}
29+
30+
int m = ans1.size();
31+
return ans1.substr(0,m-1);
32+
33+
}
34+
};

0 commit comments

Comments
 (0)