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 strategy involves taking one word as a reference and then comparing every other word to it, one character at a time. We keep track of how many characters match up perfectly across all the words, starting from the very beginning.
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 ""
# Pick the very first word from the list to be our standard for comparison.
reference_word = list_of_strings[0]
# Iterate through each character of the reference word to check it against all other words.
for character_index in range(len(reference_word)):
current_character = reference_word[character_index]
# Go through every other word in the list to check if their character matches.
for word_index in range(1, len(list_of_strings)):
current_word = list_of_strings[word_index]
# If a word is shorter or a character mismatches, the common prefix ends here.
if character_index >= len(current_word) or current_word[character_index] != current_character:
return reference_word[:character_index]
# If the loop completes, the entire first word is the common prefix.
return reference_word
The most efficient way to solve this is to realize the longest possible answer is just one of the words in the list. So, we can pick one word and carefully shorten it, character by character, until it matches the beginning of every other word in the list.
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 word as the initial guess for the longest common prefix.
longest_prefix_candidate = list_of_strings[0]
for current_index in range(1, len(list_of_strings)):
current_word_to_check = list_of_strings[current_index]
# Shorten the prefix candidate until it matches the start of the current word.
while not current_word_to_check.startswith(longest_prefix_candidate):
longest_prefix_candidate = longest_prefix_candidate[:-1]
# If the candidate becomes empty, no common prefix exists across all strings.
if not longest_prefix_candidate:
return ""
# The remaining candidate is the longest prefix common to all strings.
return longest_prefix_candidate
Case | How to Handle |
---|---|
The input array of strings is null or empty | The function should return an empty string immediately as there are no strings to compare. |
The input array contains only one string | The function should return that single string itself, as it is its own longest common prefix. |
One or more strings in the array are empty | If any string is empty, the longest common prefix for the entire array must be an empty string. |
All strings in the array are identical | The solution should correctly return any of the strings as the longest common prefix. |
No common prefix exists among the strings | The function should correctly return an empty string when the very first characters differ. |
The longest common prefix is one of the shorter strings in the array | The algorithm must not read past the end of the shortest string in the array. |
Strings containing non-alphanumeric characters or different character sets | The character-by-character comparison approach handles any valid string characters, including symbols and Unicode. |
A very large number of strings or very long strings | An efficient vertical scanning approach minimizes character comparisons and memory usage, scaling well with large inputs. |