Java solution using bit manipulation


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

Log in to reply
 

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