Java solution without bit manipulation

  • 6
        public class Solution {
            public int maxProduct(String[] words) {
                if(words==null || words.length<2)return 0;
                Word[] wordAry = new Word[words.length];
                int max =0;
                for(int i=0; i<words.length; i++){
                    wordAry[i] = new Word(words[i]);
                for(int i=0; i<wordAry.length-1; i++){
                    for(int j=i+1; j<wordAry.length; j++){
                        if(wordAry[i].shareCommon(wordAry[j]))max = Math.max(max, wordAry[i].length*wordAry[j].length);
                return max;
        class Word{
            boolean[] letters = new boolean[26];
            int length;
            Word(String s){
                for(int i=0; i<s.length(); i++){
                length = s.length();
            boolean shareCommon(Word w){
                for(int i=0; i<26; i++){
                    if(letters[i]&&w.letters[i])return false;
                return true;

  • 0

    Your code follows the exact same principle, which is simply checking which letters are common in a pair of words by checking against a boolean type for them.

  • 0

    it is wonderful to create tje new class Word , Brilliant!!

  • 1

    Even though the solution is similar to how others have done, I have nevertheless upvoted you for using your own class and implementing the methods. Superb!

Log in to reply

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