Ninja developed a love for arrays and strings so this time his teacher gave him an array of strings, ‘A’ of size ‘N’. Each element of this array is a string. The teacher taught Ninja about prefixes in the past, so he wants to test his knowledge.
A string is called a complete string if every prefix of this string is also present in the array ‘A’. Ninja is challenged to find the longest complete string in the array ‘A’.If there are multiple strings with the same length, return the lexicographically smallest one and if no string exists, return "None".
Note :String ‘P’ is lexicographically smaller than string ‘Q’, if :
1. There exists some index ‘i’ such that for all ‘j’ < ‘i’ , ‘P[j] = Q[j]’ and ‘P[i] < Q[i]’. E.g. “ninja” < “noder”.
2. If ‘P’ is a prefix of string ‘Q’, e.g. “code” < “coder”.
Example :
N = 4
A = [ “ab” , “abc” , “a” , “bp” ]
Explanation :
Only prefix of the string “a” is “a” which is present in array ‘A’. So, it is one of the possible strings.
Prefixes of the string “ab” are “a” and “ab” both of which are present in array ‘A’. So, it is one of the possible strings.
Prefixes of the string “bp” are “b” and “bp”. “b” is not present in array ‘A’. So, it cannot be a valid string.
Prefixes of the string “abc” are “a”,“ab” and “abc” all of which are present in array ‘A’. So, it is one of the possible strings.
We need to find the maximum length string, so “abc” is the required string.
The first line contains an integer 'T' which denotes the number of test cases to be run. Then the test cases follow.
The second line of each test case contains an integer ‘N’ denoting the size of array ‘A’.
The third line of each test case contains ‘N’ space-separated strings denoting the elements of array ‘A’.
Output format :
For each test case, print the longest string in ‘A’, such that every prefix of this string is also present in the array ‘A’. If there are multiple strings with the same length, return the lexicographically smallest one and if no string exists, return "None" as answer.
Print the output of each test case in a new line.
Note :
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
1 <= A[i].length <= 10^5
A[i] only consists of lowercase english letters.
Sum of A[i].length <= 10^5 over all test cases
Time Limit : 1 sec
2
6
n ni nin ninj ninja ninga
2
ab bc
ninja
None
For test case 1 we have,
All the prefixes of “ninja” -> “n”, “ni”, “nin”, “ninj” and “ninja” are present in array ‘A’. So, “ninja” is a valid answer whereas for “ninga” , the prefix “ning” is not present in array ‘A’.
So we output “ninja”.
For test case 2 we have,
The prefixes of “ab” are “a” and “ab”. “a” is not present in array ‘A’. So, “ab” is not a valid answer.
The prefixes of “bc” are “b” and “bc”. “b” is not present in array ‘A’. So, “ab” is not a valid answer.
Since none of the strings is a valid answer we output “None”.
2
5
g a ak szhkb hy
4
kez vfj vfjq vfjqo
ak
None
Try finding the prefix of each string and checking if they exist in the array.
Approach :
Algorithm :
O(Sum(A[i])*max(A[i])*log(N)), where ‘Sum(A[i])’ is the sum of length of all words ‘A[i]’, max(A[i]) is the maximum length of string in array ‘A’ and ‘N’ is the size of array ‘A’.
We are finding the prefixes of all strings ,searching them in a map, so the overall time complexity is O(Sum(A[i])*max(A[i])*N).
O(Sum(A[i])) where ‘Sum(A[i])’ is the sum of length of all words ‘A[i]’.
We are storing all strings in a map. Hence, the overall Space Complexity is O(Sum(A[i])).