leetcode

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

1239.cpp (673B)


      1 class Solution {
      2   public:
      3     int maxLength(const vector<string> &arr) const {
      4         typedef bitset<27> bs;
      5         vector<bs> vec(1, 0);
      6         int res = 0;
      7 
      8         for (const auto &s : arr) {
      9             const int n = size(s);
     10             bs crnt;
     11             for (const char c : s)
     12                 crnt.set(c & 0x1F);
     13             if (crnt.count() != n) continue;
     14             for (int i = size(vec) - 1; i >= 0; i--) {
     15                 const auto &prev = vec[i];
     16                 if ((prev & crnt).any()) continue;
     17                 res = max(res, (int)prev.count() + n);
     18                 vec.push_back(prev | crnt);
     19             }
     20         }
     21 
     22         return res;
     23     }
     24 };