| Topic | Difficulty | Companies |
|---|---|---|
| Recursion / Divide & Conquer | HARD | Amazon Google |
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
S1 and S2 is the longest string which is the prefix of both S1 and S2."-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[].