📌  相关文章
📜  最大化没有公共字符的字符串长度的乘积

📅  最后修改于: 2021-09-05 08:28:46             🧑  作者: Mango

给定一个由N 个字符串组成的数组arr[] ,任务是找到所有唯一对(i, j)的字符串arr[i]arr[j]长度的最大乘积,其中字符串arr[i]并且arr[j] 不包含公共字符。

例子:

朴素方法:解决给定问题的最简单方法是生成字符串数组的所有可能对,并打印它们之间没有公共字符的字符串对的长度乘积的最大值。

时间复杂度: O(N 2 * M),其中M字符串的最大长度
辅助空间: O(M)

高效的方法:上述方法也可以通过将每个字符串转换为其等效的位掩码整数来优化。请按照以下步骤解决问题:

  • 初始化一个变量,比如说answer ,它存储没有公共字符的字符串对的长度的最大乘积。
  • 初始化数组bits[] ,它存储数组arr[]中所有给定字符串的整数等价物。
  • [0, N – 1]范围内遍历数组arr[]并对字符串arr[i] 中的每个字符ch更新bits[i]作为bits[i](1 << (arr[i] ] – ‘a’))
  • 现在,生成所有可能的数组bits[] 对并执行以下步骤:
    • 如果比特位与[i]位[j]0,然后更新回答的值作为最大答案和组比特中的比特的计数[i]位[J]的乘积。
      否则,检查下一个可能的对。
  • 完成以上步骤后,打印值 答案为最大乘积。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
 
using namespace std;
 
// Function to count the number
// of set bits in the integer n
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
void maximumProduct(vector words)
{
     
    // Stores the integer
    // equivalent of the strings
    vector bits(words.size(), 0);
 
    // Traverse the array of strings
    for(int i = 0; i < words.size(); 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.size(); i++)
    {
        for(int j = i + 1; j < bits.size(); 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 = max(L * R, result);
            }
        }
    }
 
    // Print the maximum product
    cout << result;
}
 
// Driver Code
int main()
{
    vector arr = { "abcw", "baz", "foo",
                           "bar", "xtfn", "abcdef" };
    maximumProduct(arr);
 
    return 0;
}
 
// This code is contributed by Kingash


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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live