Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

maximum product of word

/* 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 :: common character count javascript 
Javascript :: como saber la resolucion de una ventana con javascript 
Javascript :: president zelensky 
Javascript :: datatable language 
Javascript :: scrool to top jquerry 
Javascript :: save json file python 
Javascript :: scroll to bottom of a div javascript 
Javascript :: loopback user password setter overwrite 
Javascript :: jquery :not class 
Javascript :: running scripts is disabled on this system react js 
Javascript :: jquery loop over elements 
Javascript :: nextjs x tailwind 
Javascript :: remove extra spaces javascript 
Javascript :: Codewars Returning Strings 
Javascript :: what it means --skiptests==true in angular 
Javascript :: js datetime now 
Javascript :: js detect scroll 
Javascript :: js push param to url 
Javascript :: react-native init AwesomeProject change port 
Javascript :: get moment date without time 
Javascript :: convert buffer to base64 javascript 
Javascript :: insert value to html input with javascript variable 
Javascript :: fetch url in javascript 
Javascript :: js are you sure alert 
Javascript :: node download s3 file 
Javascript :: if odd js 
Javascript :: javascript radio button change event 
Javascript :: how to use keytar electron 
Javascript :: sending form data with fetch using js 
Javascript :: jquery remove background color 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =