Find Longest Common Prefix in an Array of Strings

How to find longest common prefix in an array of strings.

Given an array of strings, write a method to find the longest common prefix string. If no common prefix is found, return an empty string ” “.

For example:

Example 1:

Input: [“cat”,”cable”,”camera”]
Output: “ca”

The longest common prefix is ca.

Example 2:

Input: [“rat”,”dog”,”elephant”]
Output: “”

No common prefix is found.