Java solution WITHOUT bit manipulation


  • 0
    E

    I used a helper method to check whether two words have unique letters:

    public class Solution {
        public int maxProduct(String[] words) {
            int result = 0;
            for (int i = 0; i < words.length; i++) {
                for (int j = i + 1; j < words.length; j++) {
                    if (haveUniqueLetters(words[i], words[j])) {
                        result = Math.max(
                            result, words[i].length() * words[j].length());
                    }
                }
            }
            return result;
        }
        
        private boolean haveUniqueLetters(String v, String w) {
            boolean[] chars = new boolean[26];
            for (int i = 0; i < v.length(); i++) {
                chars[v.charAt(i) - 'a'] = true;
            }
            for (int j = 0; j < w.length(); j++) {
                if (chars[w.charAt(j) - 'a']) return false;
            }
            return true;
        }
    }
    

Log in to reply
 

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