"Leetcode" Maximum Product of Word Lengths

Source: Internet
Author: User

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.

If this problem is compared with the violent comparison method, that is, the two-layer cycle, then the execution will time out.

You can save a key value for each word by using a Bitset method like C + +, and then compare it directly with the key value to reduce the time to compare characters when comparing words.

Like what单词abcw,我们可以创建一个l = [0,0,0.....0] 有27个0的数组,并按26个字母的顺序依次给a-z索引为1~26,最后把a,b,c,w四位对应的元素的值置为1,计算 pow(2,1)+pow(2,2)+pow(2,3)+pow(2,23)的和即为这个元素的key值。

This key value is then manipulated with the key value of the other element, and the result is 0, which indicates that the word has no identical characters.

Here's the code:

classsolution (Object): Index= []    defTranschar (self,c): l= ['a','b','C','D','e','F','g','h','I','J','k','L','m','N','o','P','Q','R','s','T','u','v','W','x','y','Z']        returnL.index (c) + 1defparsewords (self,w): t=0 L= []         forIinchRange (27): L.append (0) forIinchSet (W):#Note: Using set to filter out the same characters, I initially use w directly, resulting in a run timeoutt =Self.transchar (i) l[t]= 1T=0 forIinchRange (len (l)):ifL[i] = = 1: T= t + POW (2, i)#Print W,t        returnTdefmaxproduct (self, words):""": Type WORDS:LIST[STR]: Rtype:int"""Max=0ifLen (words) = =0:return0 L= []         forIinchWords:l.append (Self.parsewords (i)) forIinchRange (len (l)): forJinchRange (i+1, Len (l)):ifL[i] & l[j] = =0:ifMax < Len (Words[i]) *Len (Words[j]): Max= Len (words[i]) *Len (words[j])returnMax

"Leetcode" Maximum Product of Word Lengths

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.