Maximize length of the String by concatenating characters from an Array of Strings | Java program to implement the above approach ; Function to check if all the string characters are unique ; Check for repetition in characters ; Function to generate all possible strings from the given array ; Base case ; Consider every string as a starting substring and store the generated string ; Add current string to result of other strings and check if characters are unique or not ; Function to find the maximum possible length of a string ; Return max length possible ; Return the answer ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static boolean check ( String s ) { HashSet < Character > a = new HashSet < > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( a . contains ( s . charAt ( i ) ) ) { return false ; } a . add ( s . charAt ( i ) ) ; } return true ; } static ArrayList < String > helper ( ArrayList < String > arr , int ind ) { ArrayList < String > fin = new ArrayList < > ( ) ; fin . add ( " " ) ; if ( ind == arr . size ( ) ) return fin ; ArrayList < String > tmp = helper ( arr , ind + 1 ) ; ArrayList < String > ret = new ArrayList < > ( tmp ) ; for ( int i = 0 ; i < tmp . size ( ) ; i ++ ) { String test = tmp . get ( i ) + arr . get ( ind ) ; if ( check ( test ) ) ret . add ( test ) ; } return ret ; } static int maxLength ( ArrayList < String > arr ) { ArrayList < String > tmp = helper ( arr , 0 ) ; int len = 0 ; for ( int i = 0 ; i < tmp . size ( ) ; i ++ ) { len = len > tmp . get ( i ) . length ( ) ? len : tmp . get ( i ) . length ( ) ; } return len ; } public static void main ( String [ ] args ) { ArrayList < String > s = new ArrayList < > ( ) ; s . add ( " abcdefgh " ) ; System . out . println ( maxLength ( s ) ) ; } }