Maximum length of string formed by concatenation having even frequency of each character | Java Implementation of the above approach ; Function to check the string ; Count the frequency of the string ; Check the frequency of the string ; Store the length of the new String ; Function to find the longest concatenated string having every character of even frequency ; Checking the string ; Dont Include the string ; Include the string ; Driver code ; Call the function ; Print the answer
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 ( ) ) ; } }