leetcode

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

1160.cpp (710B)


      1 class Solution {
      2     static int count[27];
      3     static inline bool valid(const string &word) {
      4         static int cnt[27];
      5         memset(cnt, 0x00, sizeof(count));
      6         for (const char c : word) {
      7             const int idx = c & 0x1F;
      8             if (++cnt[idx] > count[idx]) return false;
      9         }
     10         return true;
     11     }
     12 
     13   public:
     14     int countCharacters(const vector<string> &words, const string &chars) const {
     15         memset(count, 0x00, sizeof(count));
     16         for (const char c : chars)
     17             count[c & 0x1F]++;
     18 
     19         int res = 0;
     20         for (const auto &word : words) {
     21             if (valid(word)) res += word.size();
     22         }
     23         return res;
     24     }
     25 };
     26 
     27 int Solution::count[27];