C++ 92ms code, reference to a java solution


  • 0
    1

    class Solution {
    public:
    int maxProduct(vector<string>& words) {

        if(!words.size()) return 0;
        int len=words.size();
        int *flag=new int[len];
        int *len_word=new int[len];
        int product=0;
        
        for(int j=0;j<len;j++ )
        {
            flag[j]=0;
            len_word[j]=words[j].size();
            for(int i=0;i<len_word[j];i++)
            {
                flag[j] |= 1<<(words[j][i]-'a');
            }
        }
        for(int i=0;i<len;i++)
        {
            for(int j=i+1;j<len;j++)
            {
                if(!(flag[i]&flag[j]) && len_word[i]*len_word[j]>product) product=len_word[i]*len_word[j];
            }
        }
        delete [] flag;
        delete [] len_word;
        return product;
    }
    

    };


Log in to reply
 

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