104ms solution beat 94% C++


  • 2
    X
    class Solution {
    public:
        int calculateBitMap(string word) {
            int bitMap=0;
            for(auto c : word) bitMap |= 1 << (int) (c - 'a');
            return bitMap;
        }
        
        int maxProduct(vector<string>& words) {
            if(words.size() <= 1) return 0;
            int maxLen=0;
            int n=words.size();
            
            int * bitMaps = new int[n];
            memset(bitMaps, 0, sizeof(int) * n);
            for(int i=0; i<n; i++)
                bitMaps[i] = calculateBitMap(words[i]);
            
            for(int i=0; i<n-1; i++) {
                for(int j=i+1; j<n; j++) {
                    if( ( bitMaps[i] & bitMaps[j] ) == 0 )
                        maxLen = max(maxLen, (int) (words[i].length() * words[j].length()));
                }
            }
            delete[] bitMaps;
            return maxLen;
        }
    };

Log in to reply
 

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