1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Solution { public: int maxProduct(vector<string>& words) { int n = words.size(); unordered_map<int, int> memo; int ret = 0; for(auto& str : words) { int tmp = 0; for(auto ch : str) tmp |= 1 << (ch - 'a'); memo[tmp] = max(memo[tmp], int(str.size())); } for(auto i = memo.begin(); i != memo.end(); ++i) { auto j = i; ++j; for(; j != memo.end(); ++j) if((i->first & j->first) == 0) ret = max(ret, i->second * j->second); } return ret; } };
|