Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

maximum product of word lengths leetcode solution

/* Finding the maximum product of two words Lengths in an array of words. */
//less code but slow
var maxProduct = function (words) {
    let max = 0;
    for (let i = 0; i < words.length; i++) {
        for (let j = i + 1; j < words.length; j++) {
            if (words[i].split("").filter(x => words[j].includes(x)).length === 0) {
                max = Math.max(max, words[i].length * words[j].length);
            }
        }
    }
    return max;
};

//more code but ultra fast
 var maxProduct = function(words) {
    
    const length = words.length;
    const maskes = new Array(length).fill(0);
    for (let i = 0; i < length; i++) {
        const word = words[i];
        const wordLength = word.length;
        for (let j = 0; j < wordLength; j++) {
            maskes[i] |= 1 << (word[j].charCodeAt() - 'a'.charCodeAt());
        }
    }

    
    let max = 0
     for (let i = 0; i < words.length; i++) {
        for (let j = i + 1; j < words.length; j++) {
            if ( (maskes[i] & maskes[j]) === 0 ) {
               max = Math.max(max,words[i].length*words[j].length);
            }
        }

    }
    
    return max
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: reactjs libphonenumber 
Javascript :: vuejs.org español 
Javascript :: how to generate random 6 digit charecter in js for coupon 
Javascript :: phaser place on rectangle shift 
Javascript :: phaser rotate container facing point 
Javascript :: regex to allow special characters 
Javascript :: phaser animation on complete event 
Javascript :: phaser stagger play 2 
Javascript :: complex expression in programming 
Javascript :: Pretty-Print JSON within Neovim 
Javascript :: scan token test js 
Javascript :: hook use effect with class 
Javascript :: docker for node , exoress and coackraz 
Javascript :: spiral traversal clockwise direction js 
Javascript :: Use Prototype To Add A Property To Javascript Class 
Javascript :: how to put multiple conditions in if statement node .js 
Javascript :: how to declare a variable js 
Javascript :: javascript trunc 
Javascript :: add google map in react js 
Javascript :: array objects 
Javascript :: anti aliasing 
Javascript :: nodejs 
Javascript :: how to create scroll to top button in reactjs example code 
Javascript :: donwload data from react js in json file 
Javascript :: create a reactjs app with backend and docker 
Javascript :: flatlist react native horizontal 
Javascript :: assign values to array in javascript 
Javascript :: sum of a sequence 
Javascript :: make triangle with threejs 
Javascript :: ssl certificate nodejs 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =