leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
3039.cpp (702B)
0 class Solution { 1 public: 2 string lastNonEmptyString(const string &s) const { 3 int count[27] = {0}, last[27] = {0}; 4 int maxi = 0; 5 6 for (int i = 0; i < size(s); i++) { 7 const int idx = s[i] & 0x1F; 8 maxi = max(maxi, ++count[idx]); 9 last[idx] = i; 10 } 11 12 vector<int> make; 13 for (int i = 1; i <= 26; i++) { 14 if (count[i] != maxi) continue; 15 make.push_back(i); 16 } 17 18 sort(begin(make), end(make), [&](int i, int j) { return last[i] < last[j]; }); 19 20 string res(size(make), 0); 21 for (int i = 0; i < size(make); i++) 22 res[i] = '`' + make[i]; 23 return res; 24 } 25 };