Given N strings, print the maximum length of the string and the string formed by concatenating any of the N strings, such that every letter in the string occurs even number of times
Example:
Input: N = 5, str = [“ABAB”, “ABF”, “CDA”, “AD”, “CCC”]
Output: ABABCDAADCCC 12
Explanation: The string formed by concatenation is ABABCDAADCCC. Each letter in the string occurs even number of times
Input: N = 3, str = [“AB”, “BC”, “CA”]
Output: ABBCCA 6
Explanation: The string formed by concatenation of all 3 strings is ABBCCA
Approach: The given problem can be solved using recursion and backtracking. The idea is to either include the string or exclude the string at every iteration. After including a string, the frequency of all the characters in the concatenated string is calculated. If frequency of all the characters is even we update the maximum length max. Below steps can be followed to solve the problem:
- Initialize variable max to 0 for calculating maximum length of concatenated string having even frequency of all characters
- Initialize string ans1 to store the concatenated string of maximum length with all character having even frequency
- The base case of the recursive call is to return, if index becomes equal to the size of the input string list
- At every recursive call we perform the following operation:
- Include the string and check if the frequency of characters is even for the concatenated string
- If the frequency is even, update max and ans1
- Increment the index and make the next recursive call
- Exclude the string, increment the index and make the next recursive call
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxi = 0;
string ans1 = "" ;
void calculate(string ans)
{
int dp[26] = { 0 };
for ( int i = 0; i < ans.length(); ++i) {
dp[ans[i] - 'A' ]++;
}
for ( int i = 0; i < 26; ++i) {
if (dp[i] % 2 == 1) {
return ;
}
}
if (maxi < ans.length()) {
maxi = ans.length();
ans1 = ans;
}
}
void longestString(vector<string> arr, int index,
string str)
{
if (index == arr.size()) {
return ;
}
longestString(arr, index + 1, str);
str += arr[index];
calculate(str);
longestString(arr, index + 1, str);
}
int main()
{
vector<string> A
= { "ABAB" , "ABF" , "CDA" , "AD" , "CCC" };
longestString(A, 0, "" );
cout << ans1 << " " << ans1.length();
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class index {
static int max = 0 ;
static String ans1 = "" ;
static void calculate(String ans)
{
int dp[] = new int [ 26 ];
for ( int i = 0 ; i < ans.length(); ++i) {
dp[ans.charAt(i) - 'A' ]++;
}
for ( int i = 0 ; i < dp.length; ++i) {
if (dp[i] % 2 == 1 ) {
return ;
}
}
if (max < ans.length()) {
max = ans.length();
ans1 = ans;
}
}
static void longestString(
List<String> arr, int index, String str)
{
if (index == arr.size()) {
return ;
}
longestString(arr, index + 1 , str);
str += arr.get(index);
calculate(str);
longestString(arr, index + 1 , str);
}
public static void main(String[] args)
{
ArrayList<String> A = new ArrayList<>();
A.add( "ABAB" );
A.add( "ABF" );
A.add( "CDA" );
A.add( "AD" );
A.add( "CCC" );
longestString(A, 0 , "" );
System.out.println(ans1 + " "
+ ans1.length());
}
}
|
Python3
maxi = 0 ;
ans1 = "";
def calculate(ans) :
global maxi,ans1;
dp = [ 0 ] * 26 ;
for i in range ( len (ans)) :
dp[ ord (ans[i]) - ord ( 'A' )] + = 1 ;
for i in range ( 26 ) :
if (dp[i] % 2 = = 1 ) :
return ;
if (maxi < len (ans)) :
maxi = len (ans);
ans1 = ans;
def longestString( arr, index, string) :
if (index = = len (arr)) :
return ;
longestString(arr, index + 1 , string);
string + = arr[index];
calculate(string);
longestString(arr, index + 1 , string);
if __name__ = = "__main__" :
A = [ "ABAB" , "ABF" , "CDA" , "AD" , "CCC" ];
longestString(A, 0 , "");
print (ans1, len (ans1));
|
C#
using System;
public class index {
static int max = 0;
static String ans1 = "" ;
static void calculate(String ans)
{
int [] dp = new int [26];
for ( int i = 0; i < ans.Length; ++i) {
dp[( int )ans[i] - ( int ) 'A' ]++;
}
for ( int i = 0; i < dp.Length; ++i) {
if (dp[i] % 2 == 1) {
return ;
}
}
if (max < ans.Length) {
max = ans.Length;
ans1 = ans;
}
}
static void longestString(String[] arr, int index, String str)
{
if (index == arr.Length) {
return ;
}
longestString(arr, index + 1, str);
str += arr[index];
calculate(str);
longestString(arr, index + 1, str);
}
public static void Main()
{
String[] A = { "ABAB" , "ABF" , "CDA" , "AD" , "CCC" };
longestString(A, 0, "" );
Console.WriteLine(ans1 + " " + ans1.Length);
}
}
|
Javascript
<script>
let maxi = 0;
let ans1 = "" ;
function calculate(ans) {
let dp = new Array(26).fill(0);
for (let i = 0; i < ans.length; ++i) {
dp[ans[i].charCodeAt(0) - "A" .charCodeAt(0)]++;
}
for (let i = 0; i < 26; ++i) {
if (dp[i] % 2 == 1) {
return ;
}
}
if (maxi < ans.length) {
maxi = ans.length;
ans1 = ans;
}
}
function longestString(arr, index, str) {
if (index == arr.length) {
return ;
}
longestString(arr, index + 1, str);
str += arr[index];
calculate(str);
longestString(arr, index + 1, str);
}
let A = [ "ABAB" , "ABF" , "CDA" , "AD" , "CCC" ];
longestString(A, 0, "" );
document.write(ans1 + " " + ans1.length);
</script>
|
Time Complexity: O(M*N* (2^N)), where N is the number of strings and M is the length of the longest string
Auxiliary Space: O(N)
Another Approach: The above approach can be further optimized by precomputing the frequency of characters for every string and updating the frequency array after concatenation of each string.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!