1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| class Solution { public: unsigned to_uword(const string &word) { unsigned res = 0; for (auto &&c: word) { res |= 1 << (c - 'a'); } return res; }
int maxProduct(vector<string>& words) { vector<unsigned> uwords; uwords.reserve(words.size()); for (auto &&w: words) { uwords.push_back(to_uword(w)); } int m = 0; int n = words.size(); for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { auto &s1 = uwords[i]; auto &s2 = uwords[j]; if (s1 & s2) continue; if (int r = words[i].size() * words[j].size(); r > m) m = r; } } return m; } };
|