Complete String Problem Statement
Given an array of strings A
of size N
, determine the longest complete string. A string is deemed complete if every prefix of the string also appears in the array. If multiple strings of the same maximal length exist, return the lexicographically smallest one. If no complete string exists, return "None"
.
Input:
First line contains an integer 'T' denoting the number of test cases. Each test case includes:
An integer N
indicating the size of array A
.
A list of N
space-separated strings as elements of A
.
Output:
For each test case, output the longest complete string in 'A'. If multiple strings of the same length exist, return the smallest lexicographically. If no string qualifies, return "None"
.
Example:
N = 4
A = [ "ab" , "abc" , "a" , "bp" ]
Explanation:
Prefixes of string "a" is “a” which is in 'A'.
Prefixes of "ab" are "a", "ab", both in 'A'.
"bp" has prefix "b" not in 'A'.
"abc" has prefixes "a", "ab", "abc", all in 'A'.
Thus, "abc" is the longest complete string.
Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 100000
1 ≤ A[i].length ≤ 100000
- The characters in
A[i]
are lowercase English letters. - Sum of all
A[i].length
across test cases ≤ 100000
Note:
No need to print anything, just implement the function to get the output.
AnswerBot
5d
Find the longest complete string in an array of strings, where every prefix of the string also appears in the array.
Iterate through each string in the array and check if all its prefixes exist in the ...read more
Help your peers!
Add answer anonymously...
Top ThoughtWorks Software Engineer interview questions & answers
Popular interview questions of Software Engineer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app