Question Link

https://leetcode.com/problems/maximum-product-of-word-lengths/

Question Description

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.
Input: words = [“abcw”,”baz”,”foo”,”bar”,”xtfn”,”abcdef”]
Output: 16
Explanation: The two words can be “abcw”, “xtfn”.

Simulation

Essence of this question is to do duplication validation. For duplication validation, we can have three methods, namely hashset, character array, and bit manipulation.

Solution 1:HashSet

Input: words = [“abcw”,”baz”,”foo”,”bar”,”xtfn”,”abcdef”]
Output: 16

abcw baz foo bar xtfn abcdef

Leetcode 318. Maximum Product of Word Lengths - 图1


Input: words = [“abcw”,”baz”,”foo”,”bar”,”xtfn”,”abcdef”]
Output: 16

abcw baz foo bar xtfn abcdef

abcw [11100000000000000000001000] baz[11100000000000000000000001]