43ms C++ solution (beats 87%)


  • 0
    A
    class Solution {
    public:
        
        int maxProduct(vector<string>& words) {
            vector<int> bitmask(words.size(), 0);
            
            for(int i = 0; i < words.size(); i++) {
                for(int j = 0; j < words[i].size(); j++)
                    bitmask[i] |= (1 << (words[i][j] - 'a'));
            }
            
            int ans = 0;
            for(int i = words.size()-1; i >= 0; i--) {
                for(int j = i-1; j >= 0; j--) {
                    if(!(bitmask[i] & bitmask[j])) {
                        if(ans < words[i].size()*words[j].size())
                            ans = words[i].size()*words[j].size();
                    }
                }
            }
            
            return ans;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.