leetcode

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

1297.cpp (707B)


      1 class Solution {
      2   public:
      3     int maxFreq(const string &s, int maxLetters, int sz, int _) const {
      4         unordered_map<string, int> um;
      5         static int count[26];
      6         int res = 0, uniq = 0;
      7 
      8         memset(count, 0x00, sizeof(count));
      9         for (int i = 0; i < sz; i++)
     10             if (!count[s[i] - 'a']++) uniq++;
     11         for (int i = sz; i < size(s); i++) {
     12             if (uniq <= maxLetters) um[s.substr(i - sz, sz)]++;
     13             if (!--count[s[i - sz] - 'a']) uniq--;
     14             if (!count[s[i] - 'a']++) uniq++;
     15         }
     16 
     17         if (uniq <= maxLetters) um[s.substr(size(s) - sz, sz)]++;
     18         for (const auto [_, c] : um)
     19             res = max(res, c);
     20         return res;
     21     }
     22 };