Simple c++ solution, using 26 bits in an int to indicate what characters a word has


  • 0
    I
    int maxProduct(vector<string>& words) {
        vector<int> keys;
        int ret = 0, key = 0;
        for (string& str : words) {
            key = 0;
            for (char c : str) key |= (1 << (c - 'a'));
            keys.push_back(key);
        }
        for (int i = 0; i < words.size(); i++) {
            for (int j = i + 1; j < words.size(); j++) {
                if ((keys[i] & keys[j]) == 0) {
                    ret = max((int)(words[i].size() * words[j].size()), ret);
                }
            }
        }
        return ret;
    }

Log in to reply
 

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