P17194 [GeeksforGeeks] Cf0cd86c66d07222499f84ec22dbcf6cae30e848
普及
题目描述

Note: This POTD is a part of Geek Summer Carnival. Solve all POTD consecutively from 5th to 10th April and get a chance to win exclusive discount vouchers on our GfG courses.
Geek is at the geek summer carnival. He is given an array of N strings. To unlock exclusive course discounts he needs to find the longest common prefix among all strings present in the array. Can you help him ?
Example 1:
Input:
N = 4
ar[] = {geeksforgeeks, geeks, geek, geezer}
Output:
gee
Explanation:
Longest common prefix in all the given string is gee.

Example 2:
Input:
N = 3
ar[] = {apple, ape, april}
Output:
ap
Your Task:
You don't need to read input or print anything. Complete the function LCP() that takes integer n and ar[] as input parameters and return the LCP (in case there is no common prefix return -1).

Expected time complexity: O(NlogN)
Expected space complexity: O(string length)
Constraints:
1 <= N <= 10^3
1 <= String Length <= 100

输入格式

见题面

输出格式

见题面

样例输入
N = 4 ar[] = {geeksforgeeks, geeks, geek, geezer}
样例输出
gee