Given the array of strings arr[] , write a program to find the longest common prefix string which is the prefix of all the strings in the array.

Problem Note

  • The longest common prefix for a pair of strings S1 and S2 is the longest string which is the prefix of both S1 and S2 .
  • All the given inputs are in lowercase letters a-z.
  • If there is no common prefix, return "-1" .

Example 1

Input: arr[] = [“apple", "ape", "april”] 
Output: "ap"
Explanation: The string "ap" is longest common prefix in all the strings of the given array arr[]. All the strings start with "ap", so it is the prefix.

Example 2

Input: arr[] = ["flower","fly","flight","flow"] 
Output: "fl" 
Explanation: The string "fl" is longest common prefix in all the strings of the given array arr[]. All the strings start with "fl", so it is the prefix.

Example 3

Input: arr[] = [“after”, ”academy, ”mindorks”] 
Output: “-1” 
Explanation: There is no common prefix among the strings in the array arr[].