Matching Prefix

Moderate
0/80
profile
Contributed by
2 upvotes
Asked in companies
UberInfo Edge India (Naukri.com)Amazon

Problem statement

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”.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
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.
Constraints :
1 <= T <= 10      
1 <= N <= 20
1 <= Arr[i].length <= 20

Time limit: 1 sec
Sample Input 1 :
2
8
co
code
studio
codingninjas
coding
debug
coder
cipher
3
cat
bat
rat
Sample Output 1 :
cod
bat
Explanation For Sample Input 1 :
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”.
Sample Input 2 :
2
1
ninjacoder
2
rabbit
robot
Sample Output 2 :
ninjacoder
rabbit
Hint

The string returned as the answer is a prefix string of at least one of the array strings.

Approaches (2)
Brute Force

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 :

  • Initialize minCost to INT_MAX it will store the minimum cost so far, and initialize ans to store the string to be returned.
  • Iterate each string of the array one by one, and generate all prefixes for each string.
  • Initialize curCost equal to 0, curCost will store the cost if the prefix currently generated is used as our answer.
  • To calculate curCost, iterate the strings in the array and add the length of each string to curCost.
  • While iterating each string, if the string length is greater than equal to the length of the current prefix string, check if prefix of the string is equal to the current prefix string, decrement value equal to the length of the current prefix string from curCost if this is true.
  • After checking all the strings, if curCost calculated is less than minCost then update minCost and ans, if curCost is equal to minCost then update ans if current prefix string is lexicographically smaller.
  • Finally, return the string stored in ans.
Time Complexity

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)).

Space Complexity

O( 1 )

 

No extra space is used.

Hence the space complexity is O(1).

Code Solution
(100% EXP penalty)
Matching Prefix
Full screen
Console