256ms solution using bitset<26> and new class Word


  • 0
    H
    class Word{
    public:
        int length;
        bitset<26>letters;
        Word(string s){
            length = s.length();
            for(int i=0; i<length; i++)
                letters.set(s[i]-'a');
        }
    };
    class Solution {
    public:
    int maxProduct(vector<string>& words) {
            int n = words.size();
            vector<Word*> dict(n+1);
            for(int i=0; i<n; i++)
                dict[i] = new Word(words[i]);
            int max = 0;
            int cur;
            Word * a;
            Word * b;
            for(int i=0; i<n; i++){
                a = dict[i];
                for(int j=0; j<i; j++){
                    b = dict[j];
                    if(((a->letters)&(b->letters)) == 0){
                        cur = (a->length) * (b->length);
                        if(cur>max) max = cur;
                    }
                }
            }
            return max;
    }
    };

Log in to reply
 

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