11-Lines Concise Java Code


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

Log in to reply
 

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