The Indian Engineer

Problem 318 Maximum Product of Word Lengths

Posted on 5 mins

Array String Bit-Manipulation

Problem Statement

Link - Problem 318

Question

Given a string array words, return the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. If no such two words exist, return 0.

Example 1:

Input: words = ["abcw","baz","foo","bar","xtfn","abcdef"]
Output: 16
Explanation: The two words can be "abcw", "xtfn".

Example 2:

Input: words = ["a","ab","abc","d","cd","bcd","abcd"]
Output: 4
Explanation: The two words can be "ab", "cd".

Example 3:

Input: words = ["a","aa","aaa","aaaa"]
Output: 0
Explanation: No such pair of words.

Constraints:

Solution

class Solution {
public:
    int maxProduct(vector<string>& words) {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        int size = words.size();
        vector<int> nums;
        for (int index = 0; index < size; index++) {
            int num = 0;
            for (char c : words[index]) {
                num |= 1 << (c - 'a');
            }
            nums.push_back(num);
        }
        int maxProd = 0;
        for (int index = 0; index < size; index++) {
            for (int index2 = index + 1; index2 < size; index2++) {
                if ((nums[index] & nums[index2]) == 0) {
                    maxProd = max(maxProd, (int)words[index].length() * (int)words[index2].length());
                }
            }
        }
        return maxProd;
    }
};

Complexity Analysis

| Algorithm                    | Time Complexity | Space Complexity |
| ---------------------------- | --------------- | ---------------- |
| Bitmasking with Nested Loops | O((n^2)m)       | O(n)             |

Explanation

Intial Thoughts

To solve this problem, we should first think about how to efficiently compare words to find those without common letters. We can represent each word as a unique set of letters or a bitmask. Then, we should consider how to optimize the comparison process, perhaps using a strategy that minimizes the number of word pairs to compare. Additionally, we need to keep track of the maximum product of word lengths found so far. Lastly, we must ensure the comparison is case-sensitive as per the problem constraints, but since the problem only involves lowercase letters, this simplifies our approach.

Intuitive Analysis

An intuitive approach involves recognizing the bitwise operations can efficiently represent and compare the letters in each word. By converting each word into a bitmask where each bit corresponds to the presence of a letter in the alphabet, we can use bitwise AND to check for common letters between two words. If the result is zero, it means the words do not share any letters. We should also consider sorting the words by their lengths in descending order to prioritize comparisons between longer words, which could lead to a larger product. Lastly, maintaining a running maximum of the product of word lengths as we compare pairs of words allows us to efficiently find the solution.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.