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;
}
相关推荐
深圳市快瞳科技有限公司2 分钟前
宠物行为识别:将日常行为转化为可量化的健康指标
人工智能·算法·计算机视觉·宠物
稚南城才子,乌衣巷风流21 分钟前
长链剖分(Long Chain Decomposition)算法详解
算法·深度优先·图论
变量未定义~33 分钟前
环境治理、染色时间、小蓝组网
算法
Ulyanov1 小时前
雷达导引头仿真中的坐标系——从惯性系到量测角
开发语言·python·算法·系统仿真·雷达信号处理·雷达导引头
玖玥拾1 小时前
LeetCode 13 罗马数字转整数
笔记·算法·leetcode
m沐沐1 小时前
【计算机视觉】OpenCV 物体跟踪——原理、算法与CSRT跟踪器实战
人工智能·python·深度学习·opencv·算法·计算机视觉·人脸识别
wuyk5552 小时前
66.嵌入式C语言进阶:共用体(Union)实战指南——单片机内存省一半的神级技巧
c语言·开发语言·stm32·单片机·嵌入式硬件
怪奇云呼军2 小时前
闪电智能 Voice Agent 怎样根据语速、停顿和追问方式切换话术?策略引擎拆解
开发语言·人工智能·网络协议·算法·语音识别·web app
稚南城才子,乌衣巷风流2 小时前
树链剖分(树剖)算法详解:从原理到实现
算法·深度优先
jz_ddk2 小时前
[信号处理] 数学与工程之美:相邻差分共轭乘积
数学·算法·信号处理·差分·共轭