27. Longest Common Prefix

Easy · String

Write a function that finds the longest common prefix shared by all strings in an array. A common prefix is a sequence of characters that appears at the start of every string. If no common prefix exists (not even a single character), return an empty string. The input is an array of strings, and you should return the longest common prefix as a string.

Examples

Example 1
Input: ["flower", "flow", "flight"]
Output: "fl"
Explanation: All three share the prefix "fl"
Example 2
Input: ["dog", "racecar", "car"]
Output: ""
Explanation: No common prefix

Constraints