1451.cpp (665B)
1 #pragma GCC optimize("fast") 2 static auto _ = []() { 3 ios_base::sync_with_stdio(false); 4 cin.tie(nullptr); 5 cout.tie(nullptr); 6 return 0; 7 }(); 8 9 class Solution { 10 public: 11 string arrangeWords(string &text) { 12 map<int, vector<string>> um; 13 string word, res; 14 15 text[0] = tolower(text[0]); 16 stringstream ss(text); 17 while (ss >> word) 18 um[word.size()].push_back(word); 19 20 res.reserve(text.size()); 21 for (const auto &[_, v] : um) { 22 for (const auto &w : v) 23 res += w + " "; 24 } 25 res[0] = toupper(res[0]); 26 res.pop_back(); 27 return res; 28 } 29 };