给定一个由N个字符串组成的数组arr [] ,任务是找到所有唯一对(i,j)的字符串arr [i]和arr [j]的最大长度乘积,其中字符串arr [i]和arr [j]不包含公共字符。
例子:
Input: arr[] = {“abcw”, “baz”, “foo”, “bar”, “xtfn”, “abcdef”}
Output: 16
Explanation: The strings “abcw” and “xtfn” have no common characters in it. Therefore, the product of the length of both the strings = 4 * 4 = 16, which is maximum among all possible pairs.
Input: arr[] = {“a”, “aa”, “aaa”, “aaaa”}
Output: 0
天真的方法:解决给定问题的最简单方法是生成所有可能的字符串对,并打印它们之间没有共同字符的字符串对长度乘积的最大值。
时间复杂度: O(N 2 * M),其中M是字符串的最大长度。
辅助空间: O(M)
高效方法:还可以通过将每个字符串转换为其等效的位掩码整数来优化上述方法。请按照以下步骤解决问题:
- 初始化一个变量,比如说答案,该变量存储没有共同字符的字符串对的长度的最大乘积。
- 初始化阵列位[]存储在阵列ARR所有给定的字符串的整数当量[]。
- 在数组[0,N – 1]上遍历数组arr [] ,对于字符串arr [i]中的每个字符ch ,将bit [i]更新为bit [i]和(1 <<(arr [i ] –’a’)) 。
- 现在,生成所有可能的数组位[]对,并执行以下步骤:
- 如果比特位与[i]和位[j]为0,然后更新回答的值作为最大答案和组比特中的比特的计数[i]和位[J]的乘积。
否则,请检查下一对可能的对。
- 如果比特位与[i]和位[j]为0,然后更新回答的值作为最大答案和组比特中的比特的计数[i]和位[J]的乘积。
- 完成上述步骤后,打印 答案是最终的最大积。
下面是上述方法的实现:
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to count the number
// of set bits in the integer n
public static int countSetBits(int n)
{
// Stores the count
// of set bits in n
int count = 0;
while (n > 0) {
count += n & 1;
n >>= 1;
}
// Return the count
return count;
}
// Function to find the maximum
// product of pair of strings
// having no common characters
public static void maximumProduct(
String[] words)
{
// Stores the integer
// equivalent of the strings
int[] bits = new int[words.length];
// Traverse the array of strings
for (int i = 0;
i < words.length; i++) {
// Traverse the current string
for (int j = 0;
j < words[i].length();
j++) {
// Store the current bit
// position in bits[i]
bits[i] = bits[i]
| 1 << (words[i].charAt(j)
- 'a');
}
}
// Store the required result
int result = 0;
// Traverse the array, bits[]
// to get all unique pairs (i, j)
for (int i = 0;
i < bits.length; i++) {
for (int j = i + 1;
j < bits.length; j++) {
// Check whether the strings
// have no common characters
if ((bits[i] & bits[j]) == 0) {
int L = countSetBits(
bits[i]);
int R = countSetBits(
bits[j]);
// Update the overall
// maximum product
result
= Math.max(L * R, result);
}
}
}
// Print the maximum product
System.out.println(result);
}
// Driver Code
public static void main(String[] args)
{
String arr[] = { "abcw", "baz",
"foo", "bar",
"xtfn", "abcdef" };
maximumProduct(arr);
}
}
Python3
# Python3 program for the above approach
# Function to count the number
# of set bits in the integer n
def countSetBits(n):
# Stores the count
# of set bits in n
count = 0
while (n > 0):
count += n & 1
n >>= 1
# Return the count
return count
# Function to find the maximum
# product of pair of strings
# having no common characters
def maximumProduct(words):
# Stores the integer
# equivalent of the strings
bits = [0 for i in range(len(words))]
# Traverse the array of strings
for i in range(len(words)):
# Traverse the current string
for j in range(len(words[i])):
# Store the current bit
# position in bits[i]
bits[i] = bits[i] | 1 << (ord(words[i][j]) - 97)
# Store the required result
result = 0
# Traverse the array, bits[]
# to get all unique pairs (i, j)
for i in range(len(bits)):
for j in range(i + 1, len(bits)):
# Check whether the strings
# have no common characters
if ((bits[i] & bits[j]) == 0):
L = countSetBits(bits[i])
R = countSetBits(bits[j])
# Update the overall
# maximum product
result = max(L * R, result)
# Print the maximum product
print(result)
# Driver Code
if __name__ == '__main__':
arr = [ "abcw", "baz", "foo",
"bar", "xtfn", "abcdef" ]
maximumProduct(arr)
# This code is contributed by ipg2016107
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to count the number
// of set bits in the integer n
public static int countSetBits(int n)
{
// Stores the count
// of set bits in n
int count = 0;
while (n > 0) {
count += n & 1;
n >>= 1;
}
// Return the count
return count;
}
// Function to find the maximum
// product of pair of strings
// having no common characters
public static void maximumProduct(
string[] words)
{
// Stores the integer
// equivalent of the strings
int[] bits = new int[words.Length];
// Traverse the array of strings
for (int i = 0;
i < words.Length; i++)
{
// Traverse the current string
for (int j = 0;
j < words[i].Length;
j++) {
// Store the current bit
// position in bits[i]
bits[i] = bits[i]
| 1 << (words[i][j]
- 'a');
}
}
// Store the required result
int result = 0;
// Traverse the array, bits[]
// to get all unique pairs (i, j)
for (int i = 0;
i < bits.Length; i++) {
for (int j = i + 1;
j < bits.Length; j++) {
// Check whether the strings
// have no common characters
if ((bits[i] & bits[j]) == 0) {
int L = countSetBits(
bits[i]);
int R = countSetBits(
bits[j]);
// Update the overall
// maximum product
result
= Math.Max(L * R, result);
}
}
}
// Print the maximum product
Console.WriteLine(result);
}
// Driver code
static public void Main (){
string[] arr = { "abcw", "baz",
"foo", "bar",
"xtfn", "abcdef" };
maximumProduct(arr);
}
}
// This code is contributed by offbeat
输出:
16
时间复杂度: O(N 2 ),其中M是字符串的最大长度。
辅助空间: O(N)