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 brute force approach to finding the longest common prefix is to compare the words character by character. We start by assuming the first word's initial part is the common prefix, and then we trim it down if needed by comparing it against the other words. We keep making it smaller until we find a common part or nothing is left.
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 ""
longest_common_part = list_of_strings[0]
for string_index in range(1, len(list_of_strings)):
current_string = list_of_strings[string_index]
prefix_length = 0
# Find the matching length between current prefix and string.
for char_index in range(min(len(longest_common_part), len(current_string))):
if longest_common_part[char_index] == current_string[char_index]:
prefix_length += 1
else:
break
# If no match, common prefix must be empty string
if prefix_length == 0:
return ""
# Reduce longest common prefix to the matching part
longest_common_part = longest_common_part[:prefix_length]
return longest_common_part
The best way to find the longest common prefix is to compare the words one letter at a time. We stop as soon as we find a letter that is different between any of the words. This approach is efficient because it avoids unnecessary comparisons.
Here's how the algorithm would work step-by-step:
def longest_common_prefix(list_of_strings):
if not list_of_strings:
return ""
# Start with the first string as the potential prefix
potential_prefix = list_of_strings[0]
for index in range(len(potential_prefix)):
character = potential_prefix[index]
# Compare the current character with all other strings
for other_string in list_of_strings[1:]:
# Check if index is out of bounds for the other string
if index >= len(other_string) or other_string[index] != character:
# Mismatch found, return the prefix up to this point
return potential_prefix[:index]
# If all characters match, the first string is the prefix
return potential_prefix
Case | How to Handle |
---|---|
Empty input array | Return an empty string immediately as there's no common prefix possible. |
Array containing a single empty string | The longest common prefix will be the empty string itself. |
Array with one string | The longest common prefix is the string itself. |
Strings with no common prefix | The algorithm should correctly identify this and return an empty string. |
Strings with different lengths, where one is a prefix of another | The shortest string is the longest possible common prefix, and the algorithm must handle it. |
Very long strings in the input array (potential performance issues) | Consider optimizing for character-by-character comparison to avoid unnecessary operations. |
Input strings with Unicode characters | Ensure the character comparison is Unicode-aware for correct results. |
Array containing null or undefined string values | Handle null or undefined strings appropriately, possibly by throwing an error or treating them as empty strings. |