
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Maximum length product of unique words in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of strings (only lowercase string alphabets) as the first and the only argument.
The function should pick two such strings from the array that shares no common characters and have the maximum product of their lengths. And then our function should return the length product of two such strings. If there exist no such strings in the array, we should return 0.
For example, if the input to the function is −
const arr = ["karl", "n", "the", "car", "mint", "alpha"];
Then the output should be −
const output = 20;
Output Explanation:
The words ‘mint’ and ‘alpha’ share no common word and the product of their lengths is 20.
Example
The code for this will be −
const arr = ["karl", "n", "the", "car", "mint", "alpha"]; const maxLengthProduct = (arr = []) => { const array = []; arr.forEach(str => { let curr = 0; for(let i = 0; i < str.length; i++){ curr |= 1<<(str.charCodeAt(i) - 97); }; array.push(curr); }); let res = 0; for(let i = 0 ; i < array.length; i++) { for(let j = i + 1; j < array.length ; j++) { if((array[i] & array[j]) === 0) { res = Math.max(res, arr[i].length * arr[j].length); } } } return res; }; console.log(maxLengthProduct(arr));
Output
And the output in the console will be −
20
- Related Articles
- Maximum Length of a Concatenated String with Unique Characters in C++
- Program to find maximum length of non-sharing words in Python
- Unique pairs in array that forms palindrome words in JavaScript
- Reversing the prime length words - JavaScript
- Maximum product of any two adjacent elements in JavaScript
- Finding maximum length of common subarray in JavaScript
- Reversing the even length words of a string in JavaScript
- Program to find maximum length of subarray with positive product in Python
- Maximum length subarray with LCM equal to product in C++
- JavaScript Program for Maximum Product Subarray
- Reverse only the odd length words - JavaScript
- Maximum length of mountain in an array using JavaScript
- Maximum average of a specific length of subarray in JavaScript
- Maximum Product of Two Numbers in a List of Integers in JavaScript
- Unique Morse Code Words in Python

Advertisements