318. Maximum Product of Word Lengths | 字符串长度相乘最大值

本文介绍了一种算法,用于找出字符串数组中两个没有共同字母的字符串长度乘积的最大值。通过使用位操作来快速判断两个字符串是否有共同字母,并以此为基础遍历整个数组寻找最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.

Example 1:

Given ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]
Return 16
The two words can be "abcw", "xtfn".

Example 2:

Given ["a", "ab", "abc", "d", "cd", "bcd", "abcd"]
Return 4
The two words can be "ab", "cd".

Example 3:

Given ["a", "aa", "aaa", "aaaa"]
Return 0
No such pair of words.

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

Subscribe to see which companies asked this question.

思路:创建一个int数组,长度为string数组的长度,其中每个数对应该字符串中对应位置的字符串,值是26位,最低位为a对应,最高位对应z,当该字母存在时,则对应位位1,体现在代码中则为 value[i] |= 1 << (tmp.charAt(j) - 'a');随后再遍历字符串数组,当两个字符串对应的value值相与为0,(若存在相同的字符,则相与会大于0)且长度相乘大于当前最大值,遍历完返回最大值即可。


public class Solution {
    	public int maxProduct(String[] words) {

		if (words.length == 0 || words.length == 1) {
			return 0;
		}

		int len = words.length;
		int[] value = new int[len];
		for (int i = 0; i < len; i++) {
			String tmp = words[i];
			value[i] = 0;
			for (int j = 0; j < tmp.length(); j++) {
				value[i] |= 1 << (tmp.charAt(j) - 'a');
			}

		}
		int maxProduct = 0;
		for (int i = 0; i < len - 1; i++) {
			for (int j = i + 1; j < len; j++) {
				if (((value[i] & value[j]) == 0) && words[i].length() * words[j].length() > maxProduct) {
					maxProduct = words[i].length() * words[j].length();
				}
			}
		}

		return maxProduct;
	}
}


在C语言中,我们可以使用数组来存储这十个字符串,并通过遍历找出最大长度字符串。以下是实现这个功能的一个基本示例: ```c #include <stdio.h> #include <string.h> #define MAX_STRINGS 10 void swapMaxAndLongest(char strArray[MAX_STRINGS][50], int lengths[]) { int maxIndex = 0; for (int i = 1; i < MAX_STRINGS; i++) { if (strlen(strArray[i]) > strlen(strArray[maxIndex])) { maxIndex = i; } } char longestStr = strArray[maxIndex]; int maxLength = lengths[maxIndex]; // 检查是否最大长度和最长字符串相同 if (maxLength != strlen(longestStr)) { // 如果不同,交换最大和最长字符串的位置 int lenDiff = maxLength - strlen(longestStr); memmove(&strArray[maxIndex] + lenDiff, &strArray[maxIndex], strlen(strArray[maxIndex]) + 1); // 保留结束符 memmove(&strArray[maxIndex], &longestStr, strlen(longestStr) + 1); lengths[maxIndex] = strlen(longestStr); } else { printf("最大字符串长度最长的字符串已相同,无需交换。\n"); } } int main() { char strings[MAX_STRINGS][50] = {"short", "medium", "longest string here", "another", ...}; // 你可以填充实际的字符串 int lengths[MAX_STRINGS]; // 存储每个字符串长度 for (int i = 0; i < MAX_STRINGS; i++) { lengths[i] = strlen(strings[i]); } swapMaxAndLongest(strings, lengths); // 打印结果 for (int i = 0; i < MAX_STRINGS; i++) { printf("%s (%d characters)\n", strings[i], lengths[i]); } return 0; } ``` 在这个程序中,`swapMaxAndLongest`函数负责找到最大长度字符串并判断是否需要交换。然后,在`main`函数中,我们创建了两个数组分别存放字符串和其对应的长度,然后调用`swapMaxAndLongest`函数处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值