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(wordi) * length(wordj) 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 <= wordsi.length <= 1000
  • wordsi 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;
}
相关推荐
phltxy38 分钟前
C语言操作符详解
java·c语言·算法
RuoZoe1 小时前
从 2026 年 3 月 1 日开源,到 26.10.9:Jalium UI 半年时间到底走了多远?
c语言·c++
aqiu1111111 小时前
【LeetCode 902】最大为 N 的数字组合 - 详细题解与数位组合思路
数据结构·算法·leetcode·蓝桥杯·数位dp
辰烨chenye2 小时前
LeetCode Hot 100 题解 · 哈希篇
算法·leetcode·哈希算法
罗西的思考2 小时前
DreamZero 与 DreamDojo:世界模型与策略的分层协同综合分析与对比
人工智能·算法·机器学习
玖玥拾3 小时前
LeetCode 219 存在重复元素 II
算法·leetcode·哈希算法·散列表
知无不研4 小时前
c语言中循环的介绍与简单应用
c语言·开发语言·算法·循环·for·while
a187927218315 小时前
【算法】动态规划第四篇:背包收官——min 哨兵、计数世界与组合排列分水岭
算法·leetcode·动态规划·dp·01背包·算法讲解·决策合并
HRTOS5 小时前
HRTOS 4.0 驱动库:06_Storage 存储设备驱动——24C02 EEPROM与I²C驱动开发
c语言·驱动开发·嵌入式硬件·51单片机