leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
2002.cpp (796B)
0 class Solution { 1 static int len(const string &s) { return len(s, 0, size(s) - 1); } 2 static int len(const string &s, int i, int j) { 3 int cnt = 0, maxi = 0; 4 while (i < j) { 5 if (s[i] == s[j]) 6 cnt += 2, i++, j--; 7 else 8 maxi = max(maxi, cnt + len(s, i + 1, j--)); 9 } 10 return max(maxi, cnt + (i == j)); 11 } 12 13 public: 14 int maxProduct(const string &s) const { 15 int res = 0; 16 for (int mask = 0; mask < 1 << size(s); mask++) { 17 string first, second; 18 for (int crnt = mask, idx = 0; crnt; crnt >>= 1, idx++) { 19 (crnt & 1 ? first : second) += s[idx]; 20 } 21 res = max(res, len(first) * len(second)); 22 } 23 return res; 24 } 25 };