Maximum Product of Lengths of Two Disjoint Words

You are given an array of strings words.

Find two different strings such that they do not share any common character, and return the maximum value of:

  • length(words[i]) * length(words[j])

If no such pair of strings exists, return 0.

Two strings are considered valid for pairing only if they have no lowercase English letter in common.

Example 1:

Input: words = ["abc","de","fgh","ad"]

Output: 9

Explanation:

The pair "de" and "fgh" does not share any common character.

Their product is 2 * 3 = 6.

The pair "abc" and "fgh" also does not share any common character.

Their product is 3 * 3 = 9.

So, the maximum product is 9.

Example 2:

Input: words = ["ab","cd","aef","gh"]

Output: 6

Explanation:

"ab" and "aef" cannot be chosen because both contain 'a'.

Valid pairs include:

  • "ab" and "cd" → 2 * 2 = 4
  • "ab" and "gh" → 2 * 2 = 4
  • "cd" and "aef" → 2 * 3 = 6
  • "aef" and "gh" → 3 * 2 = 6

So, the maximum product is 6.

Now Your Turn!

Pick the correct output for the given input

Input: words = ["ab","bc","de"]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 2 <= words.length <= 1000
  • 1 <= words[i].length <= 1000
  • words[i] consists only of lowercase English letters.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int maxProduct(vector<string>& words) {
// Your code goes here
}
};
Test Case

Input:

Words