Simple Java Solution


  • 0
    M
        public int maxProduct(String[] words) {
            if (words == null || words.length < 2) return 0;
            int n = words.length;
            int[] d = new int[n];
            for (int i = 0; i < n; ++i) {
                d[i] = getBits(words[i]);
            }
            int max = 0;
            for (int i = 0; i < n - 1; ++i) {
                for (int j = i + 1; j < n; ++j) {
                    if ((d[i] & d[j]) == 0) max = Math.max(max, words[i].length() * words[j].length());
                }
            }
            return max;
        }
        private int getBits(String s) {
            int x = 0;
            for (char c : s.toCharArray()) {
                x = setBit(x, c - 'a');    
            }
            return x;
        }
        private int setBit(int x, int i) {
            return x | (1 << i);
        }
    

Log in to reply
 

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