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)


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