Practice/Meta/Leetcode 14. Longest Common Prefix
CodingMust
Given an array of strings, find the longest string that is a common prefix shared by all strings in the array. If no common prefix exists, return an empty string.
A prefix is a substring that appears at the beginning of each string. For example, "pre" is a prefix of "prefix" and "prepare", but not of "suppose".
Example 1:
Input: words = ["flower", "flow", "flight"] Output: "fl" Explanation: The prefix "fl" is common to all three words.
Example 2:
Input: words = ["dog", "racecar", "car"] Output: "" Explanation: There is no common prefix among the input strings.
Example 3:
Input: words = ["interstellar", "internet", "interval"] Output: "inter" Explanation: All three words start with "inter".
Example 4:
Input: words = ["single"] Output: "single" Explanation: With only one word, the entire word is the common prefix.