Precompute masks; check all pairs with one AND each.
Approach 1
class Solution {
public int maxProduct(String[] words) {
int n = words.length, best = 0;
int[] mask = new int[n];
for (int i = 0; i < n; i++)
for (char c : words[i].toCharArray()) mask[i] |= 1 << (c - 'a');
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if ((mask[i] & mask[j]) == 0) best = Math.max(best, words[i].length() * words[j].length());
return best;
}
}
Verdict: Each pair check is O(1).
Before you submit
Edge cases and common mistakes
Test these inputs
All words share a letter (0)
Duplicate words
Mistakes people make
Comparing letter sets with nested loops over characters (O(n² × L²)).