You are given integers ‘N’ and ‘K’, and a list 'STRLIST' of ‘N’ strings. You are supposed to divide the string into exactly ‘N/K’(N will be divisible by K) groups and each group must consist of exactly ‘K’ strings. Each string must belong to exactly one group. The score of each group is equal to the length of the longest prefix that is common to all strings of that group.
You are supposed to find the maximum sum of scores of the groups.
Example :{ “abcdty”, “abcdqwse”, “abcfhjgt”, “abcdtt” }
The score for this group of strings is 3, as all strings have the longest common prefix as “abc”.
The first line contains a single integer ‘T’ denoting the number of test cases. The test cases are as follows.
The first line of each test case contains two integers ‘N’ and ‘K’ separated by a single space. ‘N’ denotes the number of strings and ‘K’ denotes the size of each group.
The next ‘N’ lines contain a single string.
Output Format:
For each test case, print a single line containing a single integer denoting the maximum sum of the scores of all groups.
The output of each test case will be printed in a separate line.
Note :
You don’t need to print anything; It has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= K <= N <= 100
1 <= |STRLIST[i]| <= 1000
“STRLIST[i]” contains only lowercase english letters.
Where ‘T’ denotes the number of test cases, ‘N’ denotes the number of strings, ‘K’ denotes the size of each group of the strings, and |STRLIST[i]| denotes the length of the i’th string.
Time Limit: 1 sec
2
2 2
hello
world
4 2
aboard
tail
above
tower
0
4
In the first test case, both the strings will be in 1 group and they don’t have any common prefix hence the answer is 0.
In the second test case, we can form the groups as { “above, “aboard” } and {“tail”, “tower”} , then the first group has a score of 3 and the second group has a score of 1. Hence the total score is 3+1=4.
2
6 3
amazing
give
amagniz
am
help
code
4 2
hello
help
home
ninja
2
3
In the first test case, it is optimal to choose one group as { “am”, “amazing”, “amginza” } and the second group as { “help”, “code”, “give” }. The first group has a score of 2 while the second group has a score of 0. Hence, the total score is 2.
In the second test case, we can form the groups as { “hello, “help” } and {“home”, “ninja”}, then the first group has a score of 3 and the second group has a score of 0. Hence the total score is 3.
Can you think about storing all prefixes of each string?
The idea is to store all the prefixes into the HashMap and then calculate the contribution of each prefix in the answer.
Consider a group of strings which has a common prefix of length ‘L’. The score for this group will be ‘L’, we can see that each prefix from 1 to ‘L’ contributes exactly 1 to the final answer. So if any prefix is present in ‘P’ groups then it will contribute exactly ‘P’ to the final answer. The problem is now reduced to finding the individual contribution of each prefix to the final answer.
The steps are as follows :
O(N * max(|S|) ^ 2), where ‘N’ is the number of strings and max(|S|) is the maximum length of the given strings.
We are inserting each prefix of all the strings into the HashMap. There are ‘N’ strings and each string can be of the length of (max|S|). There will be max(|S|) prefixes in the string and calculating each prefix will take O(max(|S|)) time on average. Also, search and insert operations in the HashMap will take time equal to the length of the string. Thus, the total time complexity will be O(N * |S| ^ 2).
O(N * max(|S|)), where ‘N’ is the number of strings and max(|S|) is the maximum length of the given strings.
We are inserting each prefix of all the strings into the HashMap. There are ‘N’ strings and each string can be of length (max|S|). There will be max(|S|) prefixes in the string. Thus there will be a total of N * (max(|S|)) prefixes in the HashMap. Thus, the overall space complexity will be O(N * max(|S|)).