Given A string array words
, find the maximum value of the where the words does not length(word[i]) * length(word[j])
share common letters. You may assume this each word would contain only lower case letters. If no such and words exist, return 0.
Example 1:
Given["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]
Return16
The both words can be "abcw", "xtfn"
.
Example 2:
Given["a", "ab", "abc", "d", "cd", "bcd", "abcd"]
Return4
The both words can be "ab", "cd"
.
Example 3:
Given["a", "aa", "aaa", "aaaa"]
Return0
No such pair of words.
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
classsolution (object):defmaxproduct (self, words): Bits_words= [Reduce (Lambdas,x:s| (1<<ord (x)-ord ('a')), W, 0) forWinchwords] ans=0 forI, Wordinchenumerate (words): forJinchRange (i+1, Len (words)):ifLen (word) *len (words[j]) > ans andSelf.has_diff (Bits_words[i], bits_words[j]): ans= Len (Word) *Len (words[j])returnansdefHas_diff (Self, W1, w2):return(w1&w2) = = 0
318. Maximum Product of Word lengths--essence: Whether a character appears in an English word can be represented by an integer of 26bit