Java Accepted solution O(n^2)


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

Log in to reply
 

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