My java code by using "&"


  • 0
    W
        int[] nums = new int[words.length];
        int[] wordLens = new int[words.length];
        for(int i=0;i<words.length;i++){
            wordLens[i] = words[i].length();
            char[] bnum = new char[26];
            for(int j=0;j<26;j++){
                bnum[j] ='0';
            }
            for(int j=0;j<wordLens[i];j++){
                bnum[words[i].charAt(j)-'a'] = '1';
            }
            nums[i] = Integer.parseInt(String.valueOf(bnum),2);
        }
        int lenPro =0;
        for(int i=0;i<words.length;i++){
            for(int j=i+1;j<words.length;j++){
                if((nums[i] & nums[j])==0){
                    int temp = wordLens[i]*wordLens[j];
                    if(temp > lenPro){
                        lenPro = temp;
                    }
                }
            }
        }
        return lenPro;

Log in to reply
 

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