C++ manipulation solution


  • 6
    class Solution {
    public:
        int maxProduct(vector<string>& words) {
            if(words.size() < 2)
            {
                return 0;
            }
            vector<int> bits(words.size(), 0);
            int result = INT_MIN;
            for(unsigned int i = 0; i < words.size(); i++)
            {
                for(unsigned int j = 0; j < words[i].length(); j++)
                {  
                    bits[i] |= 1 << (words[i][j] - 'a');
                }
            }
            for(unsigned int i = 0; i < bits.size(); i++)
            {
                for(unsigned int j = i + 1; j < bits.size(); j++)
                {
                    if(!(bits[i] & bits[j]))
                    {
                        int len = words[i].length() * words[j].length();
                        result = result >= len ? result : len;
                    }
                }
            }
            if(result == INT_MIN)
            {
                return 0;
            }
            else
            {
                return result;
            }
        }
    };

Log in to reply
 

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