requirements

Given a string array of words, find the maximum length(word[I]) * length(word[j]) that contains no common letters. You can think of each word as containing only lowercase letters. If no such two words exist, return 0.

Example 1:

Input: [abcw ", "" baz", "foo", "bar", "XTFN", "abcdef"] output: 16 explanation: the two words "abcw", "XTFN".Copy the code

Example 2:

Input: [" a ", "ab", "ABC", "d", "CD" and "BCD", "abcd"] output: 4: this two word for "ab", the "CD".Copy the code

Example 3:

Input: [" A "," AA "," AAA "," aAAA "] Output: 0 Explanation: There are no such two words.Copy the code

The core code

class Solution:
    def maxProduct(self, words: List[str]) - >int:
        words = sorted(words,key=lambda x:-len(x))
        from collections import Counter
        dic = [Counter(item) for item in words]
        res = 0
        l = len(words)
        for i in range(l - 1) :for j in range(i+1,l):
                flag = 0
                for char in words[i]:
                    if char in dic[j]:
                        flag = 1
                        break
                if flag == 0:
                    res = max(res,len(words[i]) * len(words[j]))
        return res
Copy the code

Their thinking: use double circulation way, pay attention to this part of the problem in detail, we will first is the length of the string according to the short in before long in the sort of way, the second Counter on characters, we use convenient subsequent excluding public characters out of work, in the end, the best ideas, preserved the largest value of the product.