Java O(N*N) solution


  • 0
    D
    public class Solution {
    public int maxProduct(String[] words) {
        int res  = 0;
        int n = words.length;
        int[] buf = new int[n];
        
        for(int i=0;i<n;i++)
            for(int j=0;j<words[i].length();j++)
                buf[i] = buf[i] | (1<<(words[i].charAt(j)-'a'+1));  
            
        for(int i=0;i<n;i++)
            for(int j=i+1;j<n;j++)
                if((buf[i]&buf[j]) == 0) 
                    res = Math.max(res,words[i].length() * words[j].length());
    
        return res;
    }
    

    }


Log in to reply
 

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