-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSentenceSorter.cpp
45 lines (40 loc) · 1.22 KB
/
SentenceSorter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<string>
#include<vector>
using namespace std;
struct shuffledWord {
string word = "";
int index = -1;
};
struct compShuffledWords {
inline bool operator() (const shuffledWord& w1, const shuffledWord& w2) {
return w1.index < w2.index;
}
};
class SentenceSorter {
public:
string sortSentence(string s) {
string out;
vector<shuffledWord> shuffledWords = breakIntoShuffledWords(s);
sort(shuffledWords.begin(), shuffledWords.end(), compShuffledWords());
for (int i = 0; i < shuffledWords.size(); ++i) {
out += shuffledWords[i].word + " ";
}
out.erase(out.size() - 1, 1);
return out;
}
private:
vector<shuffledWord> breakIntoShuffledWords(const string& s) {
size_t curr_pos = 0;
vector<shuffledWord> shuffledWords;
while (curr_pos < s.size()) {
size_t pos = s.find(" ", curr_pos);
if (pos == string::npos) pos = s.size();
shuffledWord sW;
sW.word = s.substr(curr_pos, pos - curr_pos - 1);
sW.index = (s.substr(pos - 1, 1))[0] - '1';
shuffledWords.push_back(sW);
curr_pos = pos + 1;
}
return shuffledWords;
}
};