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:
Imagine you are holding a group of words and you want to find the longest starting piece that is identical across every single word. The brute force strategy involves testing every possible length of the words, starting from the very beginning letter, and comparing them across all the words one by one.
Here's how the algorithm would work step-by-step:
def longest_common_prefix(word_list):
# Handle edge case where the provided word list is completely empty
if not word_list:
return ""
prefix_length = 0
first_word = word_list[0]
while prefix_length < len(first_word):
current_char = first_word[prefix_length]
for other_word in word_list:
# Stop early if we exceed bounds or find a mismatch across words
if prefix_length >= len(other_word) or other_word[prefix_length] != current_char:
return first_word[:prefix_length]
prefix_length += 1
# Return the entire first word if it is a prefix of all others
return first_wordTo find the longest shared starting piece among a group of words, the best strategy is to pick a reference word and compare it against the others one character at a time. As soon as you find a mismatch or run out of letters in any word, you know the shared beginning cannot grow any further.
Here's how the algorithm would work step-by-step:
def longest_common_prefix(string_collection):
if not string_collection:
return ""
# We establish a baseline using the first word to compare characters against all other words.
baseline_word = string_collection[0]
for char_index, current_character in enumerate(baseline_word):
# We iterate through the remaining words to ensure every word shares the exact same prefix sequence.
for other_word in string_collection[1:]:
if char_index >= len(other_word) or other_word[char_index] != current_character:
return baseline_word[:char_index]
# If the baseline word itself is fully matched across all elements, it is the longest prefix.
return baseline_word| Case | How to Handle |
|---|---|
| Empty array of strings provided as input | Return an empty string immediately since no strings exist to share a common prefix. |
| Array containing a single string | Return that single string itself as it is trivially the longest common prefix. |
| One string in the array is completely empty | Return an empty string since the intersection with an empty string yields no common prefix. |
| All strings in the array are identical | Return the first string in its entirety since it matches all other elements. |
| No common prefix exists among any of the strings | Return an empty string as the comparison terminates at the very first character mismatch. |
| One string is a strict prefix of another string | The algorithm correctly stops at the length of the shorter string and returns it. |
| Strings have vastly different lengths | Bound the comparison loops by the length of the shortest string to prevent out of bounds errors. |
| Large array with thousands of long strings | Ensure linear time complexity with respect to total characters to scale efficiently without memory overhead. |