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:
c 复制代码
// 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 word
    while (*word1) {
        letters1 |= 1 << (*word1 - 'a');
        word1++;
    }

    // Mark presence of each character in the second word
    while (*word2) {
        letters2 |= 1 << (*word2 - 'a');
        word2++;
    }

    // If there is any common letter, the AND of the two will be non-zero
    return letters1 & letters2;
}

int maxProduct(char** words, int wordsSize) {
    int maxProduct = 0;

    // Iterate through all pairs of words
    for (int i = 0; i < wordsSize; i++) {
        for (int j = i + 1; j < wordsSize; j++) {
            // Check if words[i] and words[j] share common letters
            if (!hasCommonLetters(words[i], words[j])) {
                int product = strlen(words[i]) * strlen(words[j]);
                if (product > maxProduct) {
                    maxProduct = product;
                }
            }
        }
    }

    return maxProduct;
}
相关推荐
囚生CY18 分钟前
【速写】优化的深度与广度(Adam & Moun)
人工智能·python·算法
hqyjzsb22 分钟前
2025年市场岗位能力重构与跨领域转型路径分析
c语言·人工智能·信息可视化·重构·媒体·改行学it·caie
码农多耕地呗34 分钟前
力扣94.二叉树的中序遍历(递归and迭代法)(java)
数据结构·算法·leetcode
小莞尔1 小时前
【51单片机】【protues仿真】基于51单片机智能窗帘系统
c语言·stm32·单片机·嵌入式硬件·物联网·51单片机
微笑尅乐1 小时前
BFS 与 DFS——力扣102.二叉树的层序遍历
leetcode·深度优先·宽度优先
懒羊羊不懒@1 小时前
Java基础语法—最小单位、及注释
java·c语言·开发语言·数据结构·学习·算法
SundayBear2 小时前
嵌入式进阶:C语言内联汇编
c语言·开发语言·汇编
白云千载尽2 小时前
leetcode 912.排序数组
算法·leetcode·职场和发展
哆啦刘小洋2 小时前
Tips:预封装约束的状态定义
算法
代码充电宝2 小时前
LeetCode 算法题【简单】290. 单词规律
java·算法·leetcode·职场和发展·哈希表