leetcode

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

2002.cpp (796B)


      1 class Solution {
      2     static int len(const string &s) { return len(s, 0, size(s) - 1); }
      3     static int len(const string &s, int i, int j) {
      4         int cnt = 0, maxi = 0;
      5         while (i < j) {
      6             if (s[i] == s[j])
      7                 cnt += 2, i++, j--;
      8             else
      9                 maxi = max(maxi, cnt + len(s, i + 1, j--));
     10         }
     11         return max(maxi, cnt + (i == j));
     12     }
     13 
     14   public:
     15     int maxProduct(const string &s) const {
     16         int res = 0;
     17         for (int mask = 0; mask < 1 << size(s); mask++) {
     18             string first, second;
     19             for (int crnt = mask, idx = 0; crnt; crnt >>= 1, idx++) {
     20                 (crnt & 1 ? first : second) += s[idx];
     21             }
     22             res = max(res, len(first) * len(second));
     23         }
     24         return res;
     25     }
     26 };