1process.stdin.resume(); 2process.stdin.setEncoding('utf-8'); 3let input = ''; 4 5process.stdin.on('data', (data) => { 6 input += data; 7}); 8 9process.stdin.on('end', () => { 10 let inputArray = input.trim().split(' '); // 根据题目要求,单词数组是用空格分隔的 11 12 // 辅助函数:检查两个单词是否有公共字母 13 function hasCommonLetters(word1, word2) { 14 const set1 = new Set(word1); 15 for (const char of word2) { 16 if (set1.has(char)) { 17 return true; 18 } 19 } 20 return false; 21 } 22 23 // 主函数:找到满足条件的最大乘积 24 function findMaxProduct(words) { 25 let maxProduct = 0; 26 for (let i = 0; i < words.length; i++) { 27 for (let j = i + 1; j < words.length; j++) { 28 if (!hasCommonLetters(words[i], words[j])) { 29 const product = words[i].length * words[j].length; 30 if (product > maxProduct) { 31 maxProduct = product; 32 } 33 } 34 } 35 } 36 return maxProduct; 37 } 38 39 const result = findMaxProduct(inputArray); 40 console.log(result); 41 process.exit(); 42});
Copyright ©2010-2022 比特日记 All Rights Reserved.
Powered By 可尔物语