Longest Common Prefix Problem Statement
You are given an array ‘ARR’ consisting of ‘N’ strings. Your task is to find the longest common prefix among all these strings. If there is no common prefix, you have to return an empty string.
Input:
The first line of the input contains a single integer, 'T,' denoting the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the number of strings in the array.
The next line contains ‘N’ space-separated strings denoting the elements of the array ‘ARR’.
Output:
For each test case, print a single string corresponding to the longest common prefix.
Print the output of each test case in a separate line.
Example:
Input:
ARR = [“coding”, ”codezen”, ”codingninja”, ”coders”]
Output:
"cod"
Explanation:
The longest common prefix among all the given strings is “cod” as it is present as a prefix in all strings. Hence, the answer is “cod”.
Constraints:
1 <= T <= 10
1 <= N <= 3000
1 <= |ARR[i]| <= 1000
- Each string consists of only lowercase letters.
- Time limit: 1 sec
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Be the first one to answer
Add answer anonymously...
Top APT Research Private Limited Data Analytics interview questions & answers
Popular interview questions of Data Analytics
>
APT Research Private Limited Data Analytics Interview Questions
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