当前位置: 首页 > news >正文

LeetCode //C - 318. Maximum Product of Word Lengths

318. Maximum Product of Word Lengths

Given a string array 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.
 

Example 1:

Input: words = [“abcw”,“baz”,“foo”,“bar”,“xtfn”,“abcdef”]
Output: 16
Explanation: The two words can be “abcw”, “xtfn”.

Example 2:

Input: words = [“a”,“ab”,“abc”,“d”,“cd”,“bcd”,“abcd”]
Output: 4
Explanation: The two words can be “ab”, “cd”.

Example 3:

Input: words = [“a”,“aa”,“aaa”,“aaaa”]
Output: 0
Explanation: No such pair of words.

Constraints:
  • 2 <= words.length <= 1000
  • 1 <= words[i].length <= 1000
  • words[i] consists only of lowercase English letters.

From: LeetCode
Link: 318. Maximum Product of Word Lengths


Solution:

Ideas:

1. hasCommonLetters Function:

  • This function uses bit manipulation to determine if two words share any common letters. Each word is represented by a 26-bit integer, where each bit corresponds to a letter (‘a’ to ‘z’).
  • By iterating through each character of the words and setting the corresponding bit, we can check if any bit is set in both words using the bitwise AND operator.

2. maxProduct Function:

  • The main function iterates through all pairs of words, checking if they share any common letters using the hasCommonLetters function.
  • If they don’t share any letters, it calculates the product of their lengths and updates the maxProduct if the current product is greater.

3. Efficiency:

  • The solution is optimized using bit manipulation to quickly check for common letters, making it suitable for larger input sizes within the given constraints.
Code:
// Function to check if two words share common letters
int hasCommonLetters(const char* word1, const char* word2) {int letters1 = 0, letters2 = 0;// Mark presence of each character in the first wordwhile (*word1) {letters1 |= 1 << (*word1 - 'a');word1++;}// Mark presence of each character in the second wordwhile (*word2) {letters2 |= 1 << (*word2 - 'a');word2++;}// If there is any common letter, the AND of the two will be non-zeroreturn letters1 & letters2;
}int maxProduct(char** words, int wordsSize) {int maxProduct = 0;// Iterate through all pairs of wordsfor (int i = 0; i < wordsSize; i++) {for (int j = i + 1; j < wordsSize; j++) {// Check if words[i] and words[j] share common lettersif (!hasCommonLetters(words[i], words[j])) {int product = strlen(words[i]) * strlen(words[j]);if (product > maxProduct) {maxProduct = product;}}}}return maxProduct;
}

http://www.mrgr.cn/news/5383.html

相关文章:

  • 浅谈Kafka(二)
  • 网络初识部分
  • 水土保持方案编制
  • 【17】HotSopt虚拟机的intrinsic
  • 前端vue 3中使用 顶象 vue3 版本
  • 苍穹外卖(瑞吉外卖)--环境搭建
  • C# x Unity面向对象补全计划 设计模式 之 实现一个简单的有限状态机
  • 《基于 Spark 的平替药品智能推荐方法》
  • 开发团队应对突发的技术故障和危机
  • 【nvm】误操作npm install npm@latest -g如何回退
  • SQLserver中的索引以及创建主键,外键,唯一约束,自增
  • 康耐视相机与发那科机器人通过Ethernet I/P直连与程序编写
  • wpf VisualStateManager.VisualStateGroups 介绍和举例
  • 结构型模式之外观模式
  • 美国高防服务器到底怎么选
  • 一文教你正确打通WSL和win10宿主机网络全通道
  • 鸿蒙(API 12 Beta3版)【Image Kit简介】图片处理服务
  • 互动营销小程序怎么制作
  • 122-域信息收集应用网络凭据CS插件AdfindBloodHound
  • Nginx--代理与负载均衡(扩展nginx配置7层协议及4层协议方法、会话保持)