leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1160.cpp (710B)
0 class Solution { 1 static int count[27]; 2 static inline bool valid(const string &word) { 3 static int cnt[27]; 4 memset(cnt, 0x00, sizeof(count)); 5 for (const char c : word) { 6 const int idx = c & 0x1F; 7 if (++cnt[idx] > count[idx]) return false; 8 } 9 return true; 10 } 11 12 public: 13 int countCharacters(const vector<string> &words, const string &chars) const { 14 memset(count, 0x00, sizeof(count)); 15 for (const char c : chars) 16 count[c & 0x1F]++; 17 18 int res = 0; 19 for (const auto &word : words) { 20 if (valid(word)) res += word.size(); 21 } 22 return res; 23 } 24 }; 25 26 int Solution::count[27];