Skip to content
Programming101
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Leetcode Maximum Product of Word Lengths problem solution

YASH PAL, 31 July 2024

In this Leetcode Maximum Product of Word Lengths problem solution, You are given a string array of words, return the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. If no such two words exist, return 0.

Leetcode Maximum Product of Word Lengths problem solution

Problem solution in Python.

class Solution(object):

    def maxProduct(self, words):
        """
        :type words: List[str]
        :rtype: int
        """
        word_bit = [0 for _ in range(len(words))]
        maxLen = 0
        for i, word in enumerate(words):
            for c in word:
                mask = 1 << ord(c) - ord('a')
                word_bit[i] |= mask

        for i in range(0, len(words) - 1):
            for j in range(i + 1, len(words)):
                if word_bit[i] & word_bit[j] == 0:
                    maxLen = max(len(words[i]) * len(words[j]), maxLen)

        return maxLen

Problem solution in Java.

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

Problem solution in C++.

class Solution {
public:
    int maxProduct(vector<string>& words) {
        int res=INT_MIN;
        
        for(int i=0;i<words.size();i++){
           
            for(int j=0;j<words.size() and j!=i;j++){
                if(words[i].find_first_of(words[j])==string::npos){
                    int l=words[i].length()*words[j].length();
                    res=max(res,l);
                }
            }
        }
        return max(res,0);
    }
};

Problem solution in C.

#include <stdlib.h>
#include <string.h>

struct wordData
{
    unsigned int length;
    unsigned int bitMask;
};

int maxProduct(char ** words, int wordsSize){
    int idx, idx2, maxProd = 0;
    struct wordData *pWordData;
    
    pWordData = malloc(sizeof(struct wordData) * wordsSize);
    for (idx = 0; idx < wordsSize; idx++)
    {
        char *pWord;  
        
        pWord = words[idx];
        pWordData[idx].length = 0;
        pWordData[idx].bitMask = 0;
        while ('' != *pWord)
        {
            pWordData[idx].length++;
            pWordData[idx].bitMask |= (1 << (*pWord - 'a'));
            pWord++;
        }
    }
    for (idx = 0; idx < wordsSize; idx++)
    {
        for (idx2 = idx + 1; idx2 < wordsSize; idx2++)
        {
            if (0 == (pWordData[idx].bitMask & pWordData[idx2].bitMask))
            {
                int prod;
                
                prod = pWordData[idx].length * pWordData[idx2].length;
                maxProd = (prod > maxProd) ? prod : maxProd;
            }
        }
    }
    
    free(pWordData);
    
    return maxProd;
}

coding problems solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes