Maximize length of the String by concatenating characters from an Array of Strings | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( string s ) { set < char > a ; for ( auto i : s ) { if ( a . count ( i ) ) return false ; a . insert ( i ) ; } return true ; } vector < string > helper ( vector < string > & arr , int ind ) { if ( ind == arr . size ( ) ) return { " " } ; vector < string > tmp = helper ( arr , ind + 1 ) ; vector < string > ret ( tmp . begin ( ) , tmp . end ( ) ) ; for ( auto i : tmp ) { string test = i + arr [ ind ] ; if ( check ( test ) ) ret . push_back ( test ) ; } return ret ; } int maxLength ( vector < string > & arr ) { vector < string > tmp = helper ( arr , 0 ) ; int len = 0 ; for ( auto i : tmp ) { len = len > i . size ( ) ? len : i . size ( ) ; } return len ; } int main ( ) { vector < string > s ; s . push_back ( " abcdefgh " ) ; cout << maxLength ( s ) ; return 0 ; }