leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

3039.cpp (702B)


      1 class Solution {
      2   public:
      3     string lastNonEmptyString(const string &s) const {
      4         int count[27] = {0}, last[27] = {0};
      5         int maxi = 0;
      6 
      7         for (int i = 0; i < size(s); i++) {
      8             const int idx = s[i] & 0x1F;
      9             maxi = max(maxi, ++count[idx]);
     10             last[idx] = i;
     11         }
     12 
     13         vector<int> make;
     14         for (int i = 1; i <= 26; i++) {
     15             if (count[i] != maxi) continue;
     16             make.push_back(i);
     17         }
     18 
     19         sort(begin(make), end(make), [&](int i, int j) { return last[i] < last[j]; });
     20 
     21         string res(size(make), 0);
     22         for (int i = 0; i < size(make); i++)
     23             res[i] = '`' + make[i];
     24         return res;
     25     }
     26 };