leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1451.cpp (665B)
0 #pragma GCC optimize("fast")
1 static auto _ = []() {
2 ios_base::sync_with_stdio(false);
3 cin.tie(nullptr);
4 cout.tie(nullptr);
5 return 0;
6 }();
8 class Solution {
9 public:
10 string arrangeWords(string &text) {
11 map<int, vector<string>> um;
12 string word, res;
14 text[0] = tolower(text[0]);
15 stringstream ss(text);
16 while (ss >> word)
17 um[word.size()].push_back(word);
19 res.reserve(text.size());
20 for (const auto &[_, v] : um) {
21 for (const auto &w : v)
22 res += w + " ";
23 }
24 res[0] = toupper(res[0]);
25 res.pop_back();
26 return res;
27 }
28 };