You are given an integer ’N’ denoting the length of the array ‘Arr’ of strings made up of lower case English alphabets. The cost of this array is equal to the sum of length of each string in the array.
You can select a new string ‘S’ of length ‘N’, you are now allowed to delete the prefix from the strings in the array (This leads to lowering the cost of the array), the deleted prefix should be exactly the same as the selected string ‘S.
Find the string ‘S’ such that the cost of the array ‘Arr’ is minimized. If multiple strings exist, then find the lexicographically smallest string amongst them.
For Example :If the array of strings is: {co, code, studio, codingninjas, coding, debug, coder, cipher}
Then the original cost of the array is 2 + 4 + 6 + 12 + 6 + 5 + 5 + 6 = 46.
If we select the new string as “cod” and delete the matching prefixes if exists then the array becomes: {co, e, studio, ingninjas, ing, debug, er, cipher}, and the cost now becomes: 2 + 1 + 6 + 9 + 3 + 5 + 2 + 6 = 34.
You can check for any other possible string, the cost will not become less than 34, hence the optimal answer for this case is “cod”.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains a single integer ‘N’ denoting the length of the array of strings.
The next N lines each contain ‘Arr[i]’ consisting of lower case English alphabets, denoting the ith string in the array.
Output Format :
For each test case, print the string which minimizes the cost of the array of deletion of common prefixes.
Output for each test case will be printed in a separate line.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 20
1 <= Arr[i].length <= 20
Time limit: 1 sec
2
8
co
code
studio
codingninjas
coding
debug
coder
cipher
3
cat
bat
rat
cod
bat
For test case 1 :
If we choose a string as “cod” and delete the matching prefixes from the array of strings, then the array becomes: {“co”, “e”, “studio”, “ingninjas”, “ing”, “debug”, “er”, “cipher”}.
Cost of this array is: 2 + 1 + 6 + 9 + 3 + 5 + 2 + 6 = 34, this is the lowest possible cost we can get.
For test case 2 :
If we choose a string as “bat” and delete the matching prefixes from the array of strings, then the array becomes: { “cat”, “”, “rat” }, the cost of this array is 3 + 0 + 3 = 6.
If we choose “cat” or “rat” then also we will have the cost equal to 6, but we need to return the lexicographically shortest string if multiple outputs are possible, hence we return “bat”.
2
1
ninjacoder
2
rabbit
robot
ninjacoder
rabbit
The string returned as the answer is a prefix string of at least one of the array strings.
For each string in the array consider each of its prefix string one by one, now check what will be the cost if the current prefix string is used to delete matching prefixes from all the strings in the array. Keep track of the prefix string that had the lowest cost and finally return that string.
The steps are as follows :
O( N2 * L2 ), where N and L denotes the number of strings and the maximum length of string respectively
We generate each prefix string of all the N strings in the array, there are be ~(N*L) prefix strings. Now for each prefix string generated, we try matching it with each of the N strings in the array, the matching operation takes time equal to the length of the string being matched.
Hence the time complexity is O((N*L)*(N*L)).
O( 1 )
No extra space is used.
Hence the space complexity is O(1).