leetcode

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

1002.cpp (661B)


      1 class Solution {
      2   public:
      3     vector<string> commonChars(const vector<string> &words) const {
      4         static unsigned count[26];
      5         vector<string> res;
      6 
      7         memset(count, 0xFF, sizeof(count));
      8         for (const auto &word : words) {
      9             unsigned lcount[26] = {0};
     10             for (const char c : word)
     11                 lcount[c - 'a']++;
     12             for (int i = 0; i < 26; i++)
     13                 count[i] = min(count[i], lcount[i]);
     14         }
     15 
     16         for (int i = 0; i < 26; i++) {
     17             const string c(1, 'a' + i);
     18             for (int j = 0; j < count[i]; j++)
     19                 res.push_back(c);
     20         }
     21 
     22         return res;
     23     }
     24 };