My 16ms Java Solution beat 98.83% for Maximum Product of Word Lengths


  • 3
    B
    public class Solution {
        public int maxProduct(String[] words) {
          int nLength = words.length;
    		int [] words_bit = new int [nLength];
    		int [] lenList = new int [nLength];
    		int i=0;
    		int C = 1;
    		for(String tmp:words){
    			for(char cTmp:tmp.toCharArray()){
    				words_bit[i] |=(C<<(int)(cTmp-'a'));
    			}
    			lenList[i] = tmp.length();
    			i++;
    		}
    		int max = 0;
    		for(i=0;i<nLength;i++){
    			C = (int)Math.sqrt(max);
    			for(int j=i+1;j<nLength;j++){
    				if ((lenList[i]>C||lenList[j]>C)&&((words_bit[i]&words_bit[j])==0)) {
    					max=Math.max(max, lenList[i]*lenList[j]);
    				}
    			}
    		}
    		return max;
        }
    }

  • 0
    H

    I copied and pasted your code, and the OJ ran for 33 ms...


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.