You are given an array of string ‘arr’ of size ‘N’. Your task is to find the maximum possible length of string, with unique characters formed by the concatenation of sub-sequences of ‘arr’.
For example:Consider ‘arr’ = [‘cha’, ‘r’, ‘act’, ‘ers’], the strings formed with unique characters, [‘cha’, ‘r’, ‘act’, ‘ers’, ‘char’, ‘ract’, ‘chaers’, ‘acters’]. The string with maximum length is ‘chaers’ and ‘acters’ with length 6. Hence the answer is 6.
The first line of input contains the integer ‘T’ representing the number of test cases.
The first line of each test case contains ‘N’ space-separated strings representing the element of the array.
Output Format:
For each test case, print a single integer representing the maximum length of the string of unique characters formed by the concatenation of sub-sequences of the array ‘arr’.
Print a separate line for each test case
1 <= T <= 10
1 <= N <= 26
1 <= |arr[i]| <= 26
arr[i] contains only lowercase letters.
Time Limit: 1 sec
Note:
You do not need to print anything. It has already been taken care of. Just implement the function.
2
cha r act ers
abcdefghijklmnopqrstuvwxyz
6
26
For the first test case, arr = [‘cha’, ‘r’, ‘act’, ‘ers’], the strings formed with unique characters, [‘cha’, ‘r’, ‘act’, ‘ers’, ‘char’, ‘ract’, ‘chaers’, ‘acters’]. The string with maximum length is ‘chaers’ and ‘acters’ with length 6. Hence the answer is 6.
For the second test case, arr = [‘abcdefghijklmnopqrstuvwxyz’], there is only one string in the array, and the string contains unique characters. The length of the string is 26. Hence the answer is 26.
2
abc def f
yy bkhwmpbiisbldzknpm
6
0
Try all configurations of the strings.
In this approach, we will try all the configurations of the string by using a set and applying DFS on the strings array. We will find the path, i.e., the length of the longest string of unique characters till the current position of the array, at any position.
We create dfs(strings, currString, position), where strings are the array of strings given, currString is the current string to be checked, the position is the position of the currString in the given array.
Algorithm:
O(2^N), where N is the number of strings in the array
We are iterating through every combination of strings. There are a total of 2^N combinations. Hence the overall time complexity is O(2^N).
O(N), where N is the number of strings in the array.
In the worst case, the recursion stack will take a total O(N) space. Hence the overall space complexity is O(N).