Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
Example 1:
Input: strs = ["flower","flow","flight"] Output: "fl"
Example 2:
Input: strs = ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings.
Constraints:
1 <= strs.length <= 2000 <= strs[i].length <= 200strs[i] consists of only lowercase English letters if it is non-empty.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
This problem asks for the longest string that appears at the beginning of every word in a given collection of words. The brute force method simply checks every possible starting part of a word to see if it's common to all the other words.
Here's how the algorithm would work step-by-step:
def longestCommonPrefix(list_of_strings): if not list_of_strings: return "" # The first string in the list will serve as our reference for building the prefix reference_string = list_of_strings[0] for prefix_length in range(1, len(reference_string) + 1): potential_prefix = reference_string[:prefix_length] # For each potential prefix, we must verify it's at the start of ALL other strings is_common_to_all = True for i in range(1, len(list_of_strings)): current_string = list_of_strings[i] # If the current string is shorter than the potential prefix or doesn't start with it, it's not common if len(current_string) < prefix_length or current_string[:prefix_length] != potential_prefix: is_common_to_all = False break # If the potential prefix was not common to all strings, the previous prefix was the longest common one if not is_common_to_all: return reference_string[:prefix_length - 1] # If the loop completes, the entire reference string is the longest common prefix return reference_stringThe fastest way to find the longest common prefix is to compare the words in a way that eliminates possibilities quickly. Instead of comparing every character of every word against every other word, we can focus on how the words start and stop matching.
Here's how the algorithm would work step-by-step:
def find_longest_common_prefix(list_of_strings):
if not list_of_strings:
return ""
potential_common_prefix = list_of_strings[0]
# We iterate through the rest of the strings, comparing them against the current prefix.
for current_string_index in range(1, len(list_of_strings)):
current_string = list_of_strings[current_string_index]
current_prefix_length = 0
# This loop finds the maximum length of the prefix that matches between the potential prefix and the current string.
while current_prefix_length < len(potential_common_prefix) and current_prefix_length < len(current_string) and potential_common_prefix[current_prefix_length] == current_string[current_prefix_length]:
current_prefix_length += 1
# If a string doesn't match the current prefix at all, we shorten the potential prefix.
potential_common_prefix = potential_common_prefix[:current_prefix_length]
# If the potential prefix becomes empty, there's no common prefix for all strings.
if not potential_common_prefix:
break
# The remaining potential_common_prefix is the longest shared prefix among all strings.
return potential_common_prefix| Case | How to Handle |
|---|---|
| Input array is empty | If the input array is empty, return an empty string as there are no strings to compare. |
| Input array contains only one string | If the input array has only one string, that string is the longest common prefix. |
| Input array contains empty strings | If any string in the input array is empty, the longest common prefix must be an empty string. |
| All strings in the input array are identical | The algorithm will correctly identify the entire string as the longest common prefix. |
| No common prefix exists among the strings | The algorithm will naturally terminate with an empty string if no common characters are found at the beginning. |
| Very long strings or a large number of strings | Efficiency is important; iterating character by character or sorting can be optimized to handle larger inputs within typical time limits. |
| Strings with varying lengths | The comparison should stop at the length of the shortest string to avoid index out of bounds errors. |
| Input array contains null values | A robust solution should handle null strings by treating them as if they have no characters, effectively making the common prefix empty. |