程式語言 - LeetCode - C++ - 318. Maximum Product of Word Lengths



參考資訊:
https://www.cnblogs.com/grandyang/p/5090058.html

題目:


解答:

class Solution {
public:
    int maxProduct(vector<string>& words) {
        int ans = 0;
        int n = words.size();
        vector<int> bits(n, 0);

        for (int i = 0; i < n; ++i) {
            for (char c : words[i]) {
                bits[i] |= 1 << (c - 'a');
            }

            for (int j = 0; j < i; ++j) {
                if (!(bits[i] & bits[j])) {
                    ans = max(ans, static_cast<int>(words[i].size() * words[j].size()));
                }
            }
        }

        return ans;
    }
};