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;
}
相关推荐
xyliiiiiL23 分钟前
二分算法到红蓝染色
java·数据结构·算法
珹洺2 小时前
C++从入门到实战(十)类和对象(最终部分)static成员,内部类,匿名对象与对象拷贝时的编译器优化详解
java·数据结构·c++·redis·后端·算法·链表
写bug的小屁孩2 小时前
移动零+复写零+快乐数+盛最多水的容器+有效三角形的个数
c++·算法·双指针
飞川撸码2 小时前
【LeetCode 热题100】208:实现 Trie (前缀树)(详细解析)(Go语言版)
算法·leetcode·golang·图搜索算法
这就是编程3 小时前
自回归模型的新浪潮?GPT-4o图像生成技术解析与未来展望
人工智能·算法·机器学习·数据挖掘·回归
羑悻的小杀马特3 小时前
【狂热算法篇】探寻图论幽径:Bellman - Ford 算法的浪漫征程(通俗易懂版)
c++·算法·图论·bellman_ford算法
Fantasydg6 小时前
DAY 31 leetcode 142--链表.环形链表
算法·leetcode·链表
basketball6166 小时前
C++ STL常用算法之常用排序算法
c++·算法·排序算法
moz与京6 小时前
[附C++,JS,Python题解] Leetcode 面试150题(10)——轮转数组
c++·python·leetcode
qystca7 小时前
蓝桥云客 岛屿个数
算法·dfs·bfs