📌  相关文章
📜  由每个字符的频率偶数连接形成的字符串的最大长度

📅  最后修改于: 2022-05-13 01:56:08.662000             🧑  作者: Mango

由每个字符的频率偶数连接形成的字符串的最大长度

给定N个字符串,打印字符串的最大长度以及由N个字符串,使得字符串中的每个字母出现偶数次

例子:

方法:给定的问题可以使用递归和回溯来解决。这个想法是在每次迭代时包含字符串或排除字符串。包含一个字符串后,计算连接字符串中所有字符的频率。如果所有字符的频率是偶数,我们更新最大长度max。可以按照以下步骤解决问题:

  • 将变量max初始化为 0,用于计算所有字符频率均匀的连接字符串的最大长度
  • 初始化字符串ans1以存储所有字符具有偶数频率的最大长度的连接字符串
  • 如果index等于输入字符串列表的大小,则递归调用的基本情况是返回
  • 在每次递归调用中,我们执行以下操作:
    • 包含字符串并检查连接字符串的字符频率是否均匀
      • 如果频率是偶数,更新maxans1
      • 增加索引并进行下一次递归调用
    • 排除字符串,增加索引并进行下一次递归调用

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
int maxi = 0;
string ans1 = "";
 
// Function to check the string
void calculate(string ans)
{
 
    int dp[26] = { 0 };
    for (int i = 0; i < ans.length(); ++i) {
 
        // Count the frequency
        // of the string
        dp[ans[i] - 'A']++;
    }
 
    // Check the frequency of the string
    for (int i = 0; i < 26; ++i) {
        if (dp[i] % 2 == 1) {
            return;
        }
    }
    if (maxi < ans.length()) {
 
        // Store the length
        // of the new String
        maxi = ans.length();
        ans1 = ans;
    }
}
 
// Function to find the longest
// concatenated string having
// every character of even frequency
void longestString(vector arr, int index,
                   string str)
{
 
    // Checking the string
    if (index == arr.size()) {
        return;
    }
 
    // Dont Include the string
    longestString(arr, index + 1, str);
 
    // Include the string
    str += arr[index];
 
    calculate(str);
    longestString(arr, index + 1, str);
}
 
// Driver code
int main()
{
    vector A
        = { "ABAB", "ABF", "CDA", "AD", "CCC" };
   
    // Call the function
    longestString(A, 0, "");
 
    // Print the answer
    cout << ans1 << " " << ans1.length();
 
    return 0;
}
 
// This code is contributed by Potta Lokesh


Java
// Java Implementation of the above approach
 
import java.io.*;
import java.util.*;
 
public class index {
    static int max = 0;
    static String ans1 = "";
 
    // Function to check the string
    static void calculate(String ans)
    {
 
        int dp[] = new int[26];
        for (int i = 0; i < ans.length(); ++i) {
 
            // Count the frequency
            // of the string
            dp[ans.charAt(i) - 'A']++;
        }
 
        // Check the frequency of the string
        for (int i = 0; i < dp.length; ++i) {
            if (dp[i] % 2 == 1) {
                return;
            }
        }
        if (max < ans.length()) {
 
            // Store the length
            // of the new String
            max = ans.length();
            ans1 = ans;
        }
    }
 
    // Function to find the longest
    // concatenated string having
    // every character of even frequency
    static void longestString(
        List arr, int index, String str)
    {
 
        // Checking the string
        if (index == arr.size()) {
            return;
        }
 
        // Dont Include the string
        longestString(arr, index + 1, str);
 
        // Include the string
        str += arr.get(index);
 
        calculate(str);
        longestString(arr, index + 1, str);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        ArrayList A = new ArrayList<>();
        A.add("ABAB");
        A.add("ABF");
        A.add("CDA");
        A.add("AD");
        A.add("CCC");
 
        // Call the function
        longestString(A, 0, "");
 
        // Print the answer
        System.out.println(ans1 + " "
                           + ans1.length());
    }
}


Python3
# Python3 implementation of the above approach
maxi = 0;
ans1 = "";
 
# Function to check the string
def calculate(ans) :
     
    global maxi,ans1;
     
    dp = [ 0 ] * 26;
    for i in range(len(ans)) :
 
        # Count the frequency
        # of the string
        dp[ord(ans[i]) - ord('A')] += 1;
 
    # Check the frequency of the string
    for i in range(26) :
        if (dp[i] % 2 == 1) :
            return;
         
    if (maxi < len(ans)) :
 
        # Store the length
        # of the new String
        maxi = len(ans);
        ans1 = ans;
 
# Function to find the longest
# concatenated string having
# every character of even frequency
def longestString( arr,  index, string) :
 
 
    # Checking the string
    if (index == len(arr)) :
        return;
 
    # Dont Include the string
    longestString(arr, index + 1, string);
 
    # Include the string
    string += arr[index];
 
    calculate(string);
    longestString(arr, index + 1, string);
 
 
# Driver code
if __name__ == "__main__" :
 
    A = [ "ABAB", "ABF", "CDA", "AD", "CCC" ];
   
    # Call the function
    longestString(A, 0, "");
 
    # Print the answer
    print(ans1, len(ans1));
 
    # This code is contributed by AnkThon


C#
// C# Implementation of the above approach
using System;
 
public class index {
    static int max = 0;
    static String ans1 = "";
 
    // Function to check the string
    static void calculate(String ans)
    {
 
        int[] dp = new int[26];
        for (int i = 0; i < ans.Length; ++i) {
 
            // Count the frequency
            // of the string
            dp[(int)ans[i] - (int)'A']++;
        }
 
        // Check the frequency of the string
        for (int i = 0; i < dp.Length; ++i) {
            if (dp[i] % 2 == 1) {
                return;
            }
        }
        if (max < ans.Length) {
 
            // Store the Length
            // of the new String
            max = ans.Length;
            ans1 = ans;
        }
    }
 
    // Function to find the longest
    // concatenated string having
    // every character of even frequency
    static void longestString(String[] arr, int index, String str)
    {
 
        // Checking the string
        if (index == arr.Length) {
            return;
        }
 
        // Dont Include the string
        longestString(arr, index + 1, str);
 
        // Include the string
        str += arr[index];
 
        calculate(str);
        longestString(arr, index + 1, str);
    }
 
    // Driver code
    public static void Main()
    {
        String[] A = {"ABAB", "ABF", "CDA", "AD", "CCC"};
 
        // Call the function
        longestString(A, 0, "");
 
        // Print the answer
        Console.WriteLine(ans1 + " " + ans1.Length);
    }
}
 
// This code is contributed by saurabh_jaiswal.


Javascript


输出
ABABCDAADCCC 12


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

另一种方法:通过预先计算每个字符串的字符频率并在每个字符串连接后更新频率数组,可以进一步优化上述方法。