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 <= 200
0 <= strs[i].length <= 200
strs[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:
The goal is to find the longest string that appears at the beginning of every word in a given list. The brute force method achieves this by exhaustively comparing each possible prefix against all the words in the list. We start with the shortest possible prefix and extend it until we find a mismatch.
Here's how the algorithm would work step-by-step:
def longest_common_prefix_brute_force(list_of_strings):
if not list_of_strings:
return ""
first_word = list_of_strings[0]
longest_common_prefix = ""
for index in range(1, len(first_word) + 1):
potential_prefix = first_word[0:index]
# Iterate through all other strings in the list
for other_string in list_of_strings[1:]:
# Check if current string starts with prefix
if not other_string.startswith(potential_prefix):
# If a mismatch occurs, return
return longest_common_prefix
# If all strings contain prefix, then update
longest_common_prefix = potential_prefix
return longest_common_prefix
The most efficient way to find the longest common prefix is to compare the words in a smart way, one letter at a time. We'll start with the first word and see how much of it matches the beginning of all the other words.
Here's how the algorithm would work step-by-step:
def longest_common_prefix(list_of_strings):
if not list_of_strings:
return ""
prefix = list_of_strings[0]
# Iterate through the characters of the first string.
for character_index in range(len(prefix)):
character = prefix[character_index]
# Compare char with the chars in other strings.
for string_index in range(1, len(list_of_strings)):
# If the current string is shorter or chars don't match.
if character_index >= len(list_of_strings[string_index]) or \
list_of_strings[string_index][character_index] != character:
# Return the prefix up to the current index.
return prefix[:character_index]
# If the first word is the prefix.
return prefix
Case | How to Handle |
---|---|
Empty input array | Return an empty string immediately, as there are no strings to compare. |
Array containing only an empty string | Return an empty string, as an empty string is the longest common prefix. |
Array containing strings of different lengths where the shortest string is empty | Return an empty string, as no prefix can be formed. |
Array with one string | Return the single string itself, as it is trivially the longest common prefix. |
All strings are identical | Return any of the strings, as they are all the longest common prefix. |
No common prefix exists | Return an empty string when the loop completes without finding a common prefix. |
Very long strings with a short common prefix | The algorithm should efficiently find the short prefix without needing to compare the entire long strings. |
Array with null strings | Handle null string inputs by either skipping them or throwing an exception, depending on requirements (skipping is often safer). |